1

I need to convert the following array into *correction an object however I also need to append something before each object is printed.

var arrayList = ["image1.jpg","image2.jpg","image3.jpg"];

I need to turn this into the following...

{src: 'image1.jpg'},{src: 'image2.jpg'},{src: 'image3.jpg'}

I have tried things like:

{src: '"+ arrayList.toString+"'}

However this just outputs all 3 in one long string. I know that I need to use a loop or jquery each() statement however I'm just not sure how to do that and get my final output that I need.

Any help would be greatly appreciated!

3
  • 1
    So are you trying to create an object or a string, it's not really clear ? Commented Apr 1, 2014 at 18:52
  • Very sorry I meant to object but said string. I made the corrections to my question. Commented Apr 1, 2014 at 19:21
  • Thank you all very much for your fast response... as always love the community. Commented Apr 1, 2014 at 19:23

3 Answers 3

2

For an object

var arrayList = ["image1.jpg","image2.jpg","image3.jpg"];

var obj = arrayList.map(function(item) {
    return {src: item};
});

and to turn that into a string

var str = JSON.stringify(obj);

or if you just need a string (which makes very little sense as it's not accessible at all ?)

var str = '{src : \'' + arrayList.join('\'}, {src : \'') + '\'}';

FIDDLE

Sign up to request clarification or add additional context in comments.

7 Comments

Don't forget the quotation marks around each image_.jpg.
@cookiemonster - Yeah, that's probably very important, especially the single quotes, so it can't be parsed as JSON or used for anything really ?
I was just pointing out that the result didn't match the requirement. It's ridiculous to suggest that the data can't be used for anything. Creating custom serialization isn't uncommon.
@cookiemonster - I can't say that I've ever seen a string like this used for anything? A string that looks like an object but is a string, with single quotes so it's not valid JSON? Just seems too weird, and I was right in this case, but of course it could be used for something or passed to something that had some sort of strange custom parser instead of using the built in stuff, but I wouldn't say it's very common.
@cookiemonster - Lets agree to agree on that one !
|
2

Use map to modify each item as desired, then finally join to create your entire string.

arrayList.map(function (e) {return '{src: \'' + e + '\'}';}).join();
// "{src: 'image1.jpg'},{src: 'image2.jpg'},{src: 'image3.jpg'}"

Please notice that though the output looks like Objects, this is only making Strings.

1 Comment

@whiteb0x - That's a bold statement as the question is not very clear, but generally a string that looks exactly like an object sounds rather useless, so I'd say the OP doesn't really know what he wants.
1

My version:

$(document).ready(function(){
var arrayList = ["image1.jpg","image2.jpg","image3.jpg"];
var Lista = "";
var Top = arrayList.length;
var cont = 1;
$.each(arrayList,function(index, value){
    Lista = Lista + "{src:'"+value+"'}";
    if(cont < Top)
    {
     Lista = Lista + ",";
     cont++;
    }
});
console.log(Lista);
});

Working fiddle: http://jsfiddle.net/robertrozas/sV8Fc/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.