3 回答
TA贡献1816条经验 获得超4个赞
由于您似乎想将spotify用作links.
JS中有两种类型的数组:
标准数组是: [ ]
关联数组是:{ }!
如您所见,您可以将{ }which 也用作 javascript 中的对象作为数组。然后你就可以用它spotify作为钥匙了。所以你的代码将如下所示:
var links = {};
$.each(playlist_data, function(index, element) {
links[index] = {
spotify: element.spotify,
soundcloud: element.soundcloud,
};
});
console.log(links_s.Music1.spotify) // Spotify Music 1
TA贡献1895条经验 获得超3个赞
你可以用下面的代码命名你的每个 obj
var playlist_data = {
"Music1": {
"soundcloud": "Soundcloud Music 1",
"spotify": "Spotify Music 1"
},
"Music2": {
"soundcloud": "Soundcloud Music 2",
"spotify": "Spotify Music 2"
},
"Music3": {
"soundcloud": "Soundcloud Music 3",
"spotify": "Spotify Music 3"
},
"Music4": {
"soundcloud": "Soundcloud Music 4",
"spotify": "Spotify Music 4"
}
};
var links = [];
$.each(playlist_data, function(index, element) {
links.push({
spotify: element.spotify,
soundcloud: element.soundcloud,
});
});
let myMusic = {};
for(let x = 0 ; x < links.length ; x++)
{
let z = Number(x+1);
myMusic["music"+z] = links[x];
}
console.log(myMusic.music1);
添加回答
举报