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.
-
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 thefor
loop. – Dekel Commented May 22, 2017 at 21:22 -
var src = $.map(images, x => {src : x})
– adeneo Commented May 22, 2017 at 21:25
3 Answers
Reset to default 6Your 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
版权声明:本文标题:Push A List of Objects Into an Array Using jQuery (Or Javascript) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744708859a2621005.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论