admin管理员组

文章数量:1389867

$=jQuery.noConflict();
$( document ).ready(function() {
    //returns an array of image links
    // [".jpg", ".jpg", ".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.

$=jQuery.noConflict();
$( document ).ready(function() {
    //returns an array of image links
    // ["http://velnikolic./gallery/wp-content/uploads/2017/04/file4741298583098-1-150x150.jpg", "http://velnikolic./gallery/wp-content/uploads/2017/04/file9221293737060-150x150.jpg", "http://velnikolic./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.

Share Improve this question edited May 22, 2017 at 21:47 Jeffrey Chung 19.5k8 gold badges35 silver badges54 bronze badges asked May 22, 2017 at 21:16 Velibor NikolicVelibor Nikolic 2373 silver badges15 bronze badges 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. – Dekel Commented May 22, 2017 at 21:22
  • var src = $.map(images, x => {src : x}) – adeneo Commented May 22, 2017 at 21:25
Add a ment  | 

3 Answers 3

Reset to default 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);

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./gallery/wp-content/uploads/2017/04/file4741298583098-150x150.jpg"},
    {src: "http://velnikolic./gallery/wp-content/uploads/2017/04/file9221293737060-150x150.jpg"}, 
    {src: "http://velnikolic./gallery/wp-content/uploads/2017/04/file4741298583098-150x150.jpg"}
]
//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.

本文标签: Push A List of Objects Into an Array Using jQuery (Or Javascript)Stack Overflow