javascript通过多维对象读取
我有一个javascript中的对象,看起来像这样:javascript通过多维对象读取,javascript,arrays,object,Javascript,Arrays,Object,我有一个javascript中的对象,看起来像这样: Cards:Object |->Pack:Object |->Spades:Array[60] |-> 0: Object |-> Card_img: "www.test1.com" Card_type:"9"
Cards:Object
|->Pack:Object
|->Spades:Array[60]
|-> 0: Object
|-> Card_img: "www.test1.com"
Card_type:"9"
1:
|-> Card_img:"www.test2.com"
Card_type:"8"
如何循环使用spades{0,1等}中的所有键->值属性?所以它会返回给我类似{card_img:www.test1.com,card_img:www.test2.com}
我的代码:
如果我使用Object.keyscards.Packs.forEachfunction key,我会在非Object上调用Object.keys您不能拥有具有重复键的对象,但可以将每个卡存储在一个数组中
fori in Cards.Pack.Spades{…}Spades是一个数组,对吗?为什么不通过数值索引遍历数组呢?对于var i=0;i
Object.keys(cards).forEach(function (key) {
console.log(key); //gives Card how to drill down further?
});
// a dummy cards object
var cards = {
pack: {
spades: [
{ card_img: 'blah.png', card_type: '9' },
{ card_img: 'blah.png', card_type: '10' },
]
}
};
// array to hold the spades
var spades = [];
// loop through each of the "pack" properties
for (i in cards.pack) {
// loop through each of the cards of this suit
for (j in cards.pack[i]) {
spades.push(cards.pack[i][j]);
}
}
// print out your cards
console.log(spades);