我如何有效地计算当前对象索引中特定对象属性的.length?
我是否需要遍历每个content.item
索引,然后遍历content.item.itemSettings.item.monsters
并在途中进行“手动”计算?
在下面我试图计算每个特定属性
content.item
我试图计算的属性是“怪物”:
content.item.itemSettings.item.monsters
var content = {
item: [{
mainId: 50,
itemSettings: {
item: [{
level: 1,
monsters: [{
name: 'monster 1',
item: [{
innerLabel: 'blue',
year: [{
yearVal: 2016,
yearStat: 70,
},
{
yearVal: 2012,
yearStat: 30,
}
]
}]
},
{
name: 'monster 2',
item: [{
innerLabel: 'pink',
year: [{
yearVal: 2016,
yearStat: 20,
},
{
yearVal: 2012,
yearStat: 90,
}
]
}]
},
]
},
{
level: 1,
monsters: [{
name: 'monster 3',
item: [{
innerLabel: 'gray',
year: [{
yearVal: 2016,
yearStat: 50,
},
{
yearVal: 2012,
yearStat: 55,
}
]
}]
},
{
name: 'monster 4',
item: [{
innerLabel: 'red',
year: [{
yearVal: 2016,
yearStat: 20,
},
{
yearVal: 2012,
yearStat: 100,
}
]
}]
},
]
},
] //mainId
}, //itemSettings
}, //content.item
//NEW
{
mainId: 60,
itemSettings: {
item: [{
level: 2,
monsters: [{
name: 'monster 2-1',
item: [{
innerLabel: 'blue',
year: [{
yearVal: 2016,
yearStat: 70,
},
{
yearVal: 2012,
yearStat: 30,
}
]
}]
},
{
name: 'monster 2-2',
item: [{
innerLabel: 'pink',
year: [{
yearVal: 2016,
yearStat: 20,
},
{
yearVal: 2012,
yearStat: 90,
}
]
}]
},
]
},
{
level: 2,
monsters: [{
name: 'monster 2-3',
item: [{
innerLabel: 'gray',
year: [{
yearVal: 2016,
yearStat: 50,
},
{
yearVal: 2012,
yearStat: 55,
}
]
}]
}, ]
},
] //mainId
}, //itemSettings
}, //content.item
]
}; //content
答案 0 :(得分:0)
您可以使用reduce
。 (如果您对代码中的任何内容一无所知,请询问)
var length = content.item.reduce((sum, value) => {
return sum + Object.values(value.itemSettings.item).reduce((isum, ivalue) => {
return isum + ivalue.monsters.length;
}, 0);
}, 0);
工作代码:
var content = {
item: [
{
mainId: 50,
itemSettings: {
item: [{
level: 1,
monsters: [{
name: "monster 1",
item: [{
innerLabel: "blue",
year: [{
yearVal: 2016,
yearStat: 70,
},
{
yearVal: 2012,
yearStat: 30,
}
]
}]
},
{
name: "monster 2",
item: [{
innerLabel: "pink",
year: [{
yearVal: 2016,
yearStat: 20,
},
{
yearVal: 2012,
yearStat: 90,
}
]
}]
},
]
},
{
level: 1,
monsters: [{
name: "monster 3",
item: [{
innerLabel: "gray",
year: [{
yearVal: 2016,
yearStat: 50,
},
{
yearVal: 2012,
yearStat: 55,
}
]
}]
},
{
name: "monster 4",
item: [{
innerLabel: "red",
year: [{
yearVal: 2016,
yearStat: 20,
},
{
yearVal: 2012,
yearStat: 100,
}
]
}]
},
]
},
]
},
},
{
mainId: 60,
itemSettings: {
item: [{
level: 2,
monsters: [{
name: "monster 2-1",
item: [{
innerLabel: "blue",
year: [{
yearVal: 2016,
yearStat: 70,
},
{
yearVal: 2012,
yearStat: 30,
}
]
}]
},
{
name: "monster 2-2",
item: [{
innerLabel: "pink",
year: [{
yearVal: 2016,
yearStat: 20,
},
{
yearVal: 2012,
yearStat: 90,
}
]
}]
},
]
},
{
level: 2,
monsters: [{
name: "monster 2-3",
item: [{
innerLabel: "gray",
year: [{
yearVal: 2016,
yearStat: 50,
},
{
yearVal: 2012,
yearStat: 55,
}
]
}]
}, ]
},
]
},
}
]
};
var length = content.item.reduce((sum, value) => {
return sum + Object.values(value.itemSettings.item).reduce((isum, ivalue) => {
return isum + ivalue.monsters.length;
}, 0);
}, 0);
console.log(length);

答案 1 :(得分:0)
如果您需要单独指定每个索引的长度,可以使用以下代码:
content.item.map(item =>
item.itemSettings.item.reduce((count, itemInner) =>
count + itemInner.monsters.length, 0
)
);
输出:[4, 3]