const json = [{
"order": 1111,
"items": [
{
"colour1": "red",
"colour2": "yellow",
},
{
"colour1": "red",
"colour2": "red",
},
{
"colour1": "red",
"colour2": "red",
}
]
},
{
"order": 2222,
"items": [
{
"colour1": "black",
"colour2": "blue",
"colour3": "orange"
},
{
"colour1": "white",
"colour2": "red",
"colour3": "green",
}
]
}]
Object.entries(json).forEach(([i, v]) => {
let count = [];
Object.entries(v.items).forEach(([j, k]) => {
if (k.colour2.includes('red')) {
count.push(k.colour2)
}
});
console.log(count, count.length) //length = [2, 1]
});
我觉得我写的这段代码不是过滤和计算长度的有效方法。目标是过滤某个值并得到结果。寻找替代方法和正确的 es6 方法来做到这一点。谢谢
幕布斯6054654
潇湘沐
烙印99
相关分类