2 回答
TA贡献1848条经验 获得超6个赞
使用Array.reduce你可以提取 only 的数组ItemC,并且可以很容易地找到那里的项目。
let input = [{
"ItemAID" : 1,
"ItemADesc" : [ {
"ItemBid" : 11,
"ItemBDesc" : [ {
"ItemCid" : 111,
"ItemCTitle" : "TitleC111",
}, {
"ItemCid" :112,
"ItemCTitle" : "TitleC112",
} ]
}]},
{
"ItemAID" : 2,
"ItemADesc" : [ {
"ItemBid" : 21,
"ItemBDesc" : [ {
"ItemCid" : 211,
"ItemCTitle" : "TitleC211",
}, {
"ItemCid" :212,
"ItemCTitle" : "TitleC212",
} ]
}]}];
const itemC = input.reduce((acc, cur) => {
if (cur['ItemADesc'] && cur['ItemADesc'].length > 0) {
cur['ItemADesc'].forEach(({ ItemBDesc }) => {
if (ItemBDesc.length > 0) {
acc.push(...ItemBDesc);
}
});
}
return acc;
}, []).find(({ ItemCid }) => ItemCid === 211);
console.log(itemC.ItemCTitle);
添加回答
举报