如何创建一个多维json数组

时间:2021-12-08 21:15:22
var items = [{
    title: 'sample 1',
    image: 'http://www.lorempixel.com/700/600/'
}, {
    title: 'sample 2',
    image: 'http://www.lorempixel.com/900/1200/'
}, {
    title: 'sample 3',
    image: 'http://www.lorempixel.com/400/300/'
}, {
    title: 'sample 4',
    image: 'http://www.lorempixel.com/600/600/'
}, {
    title: 'sample 5',
    image: 'http://www.lorempixel.com/400/310/'
}, {
    title: 'sample 6',
    image: 'http://www.lorempixel.com/410/300/'
}, {
    title: 'sample 7',
    image: 'http://www.lorempixel.com/500/300/'
}, {
    title: 'sample 8',
    image: 'http://www.lorempixel.com/300/300/'
}, {
    title: 'sample 9',
    image: 'http://www.lorempixel.com/450/320/'
}, {
    title: 'sample 10',
    image: 'http://www.lorempixel.com/500/400/'
}];

Instead of hard coding this, I would like to create this exact same array dynamically - here is my code.

而不是硬编码,我想动态创建这个完全相同的数组 - 这是我的代码。

for(var key in pics) {
    var items[];
    items.push(pics[key].source);
}

I dont think this works because it just pushes the images into a standard array like this:

我不认为这是有效的,因为它只是将图像推送到这样的标准数组:

items = [1.jpg, 2.jpg....];

How can I accomplish this,cheers.

我怎么能做到这一点,干杯。

1 个解决方案

#1


1  

I don't know where you get the img url and i assume pics.source is the title:

我不知道你在哪里获得img url,我认为pics.source是标题:

var items = [];
for(var key in pics) {
    items.push({title: pics[key].source, image: <img url>});
}

#1


1  

I don't know where you get the img url and i assume pics.source is the title:

我不知道你在哪里获得img url,我认为pics.source是标题:

var items = [];
for(var key in pics) {
    items.push({title: pics[key].source, image: <img url>});
}