how to multidimesional array as follows in javascript
I want to make array like this
var ImageArray = [
{image:"/image1.jpg"},
{image:"/image1.jpg"},
{image:"/image开发者_StackOverflow1.jpg"}
]
I want to make above structure of array from div containing images using each function of jquery.
so that I can retrieve it like ImageArray[index].image
var ImageArray = [];
$('div img').each(function(){
ImageArray.push({image:this.src});
});
this div
part of the selector should be altered to match the div you want to use as the container..
You can also use jQuery.map()
to do it in a single call, without having to declare a separate array, e.g.:
var ImageArray = $('div img').map(function(i,img) {
return {image:this.src};
});
var ImageArray = [];
$('div.images').children('img').each( function() {
ImageArray.push({ image: this.src }) // or $(this).attr('src')
} );
精彩评论