0
$=jQuery.noConflict();
$( document ).ready(function() {
    //returns an array of image links
    // ["http://velnikolic.com/gallery/wp-content/uploads/2017/04/file4741298583098-1-150x150.jpg", "http://velnikolic.com/gallery/wp-content/uploads/2017/04/file9221293737060-150x150.jpg", "http://velnikolic.com/gallery/wp-content/uploads/2017/04/file4741298583098-150x150.jpg"]
    var images = <?php echo json_encode($images); ?>;
    console.log(images);
    src = [];
    data = {}

    for (var i = 0; i < images.length; i++) {
        data = {
            src: images[i] 
        };

        src.push(data);
        console.log(data);
        //Data should equal [{src : imageurlxxx}, {src :imgurlxxdd}, {src :imgurlxxdd} ]}
    }
});//close

The above code should loop through the images array and push it into an object with src as the key, then it should push this object into an array and reiterate. The problem is that the object is overwritten in the array because they all have the same key.

2
  • Actually - data should only equal to an object with one key, and not to what you are looking for. The src variable should have what you are looking for, and it will be after the for loop. Commented May 22, 2017 at 21:22
  • var src = $.map(images, x => {src : x}) Commented May 22, 2017 at 21:25

3 Answers 3

6

Your code does exactly what you are looking for, but you checked the wrong variable:

var images = ["A", "B", "C"]
console.log(images);
src = [];
data = {}
for (var i = 0; i < images.length; i++) {
  data = {
    src: images[i] 
  };
  // Here data is an object with 1 key only.

  src.push(data);
}
// Here - after the loop - the src variable will contain all of the values that you want.
console.log(src);

And if you are looking for an ES6 solution you can use this one:

var images = ["A", "B", "C"];
var src = images.map((img) => { return {src: img} });
console.log(src);

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

1 Comment

@TxRegex the images are already javascript-object, and not a string
1
function getImages() {
    var images = <?php echo json_encode($images); ?>;
    return images.map(function (image) {
        return { src: image };
    }
};

Assuming you are using this on a php site and $images is set, getImages() returns something like this:

 [
    {src: "http://velnikolic.com/gallery/wp-content/uploads/2017/04/file4741298583098-150x150.jpg"},
    {src: "http://velnikolic.com/gallery/wp-content/uploads/2017/04/file9221293737060-150x150.jpg"}, 
    {src: "http://velnikolic.com/gallery/wp-content/uploads/2017/04/file4741298583098-150x150.jpg"}
]

Comments

0
//Data should equal [{src : imageurlxxx}, {src :imgurlxxdd}, {src :imgurlxxdd} ]}
data = $.map(images, (image) => { src: image })

If i'm reading what you want right, you just need to map your image urls into the array like you want them.

1 Comment

There is no need for jquery map here, you can use Array.map function (you can check the example in my answer)

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.