这是我从后端获得的ajax响应。我需要搜索属性“select”:这个响应是真的。
来自后端的Ajax响应:
*ngSwitchCase="'text'"
这是预期的输出:
[
[
{
"itemCategory": "Bedroom",
"task": "Bedroom",
"children": [
{
"task": "Smart TV",
"price": 300,
"leaf": true,
"labor": "",
"rate": 0,
"text": ""
},
{
"task": "Bed",
"price": 500,
"leaf": true,
"labor": "",
"rate": 0,
"text": ""
}
]
},
{
"itemCategory": "Kitchen",
"task": "Kitchen",
"children": [
{
"task": "Micro oven",
"price": 200,
"leaf": true,
"labor": "",
"rate": 0,
"text": ""
},
{
"task": "Refrigerator",
"price": 250,
"leaf": true,
"labor": "",
"rate": 0,
"text": ""
}
]
},
{
"itemCategory": "Kitchen",
"task": "Kitchen",
"children": [
{
"task": "Dining Table",
"price": 100,
"leaf": true,
"labor": "",
"rate": 0,
"text": ""
}
]
}
],
[
{
"itemCategory": "Bedroom",
"task": "Bedroom",
"children": [
{
"task": "Smart TV",
"price": 300,
"leaf": true,
"labor": "5",
"rate": 4,
"text": "",
"select": true
},
{
"task": "Bed",
"price": 500,
"leaf": true,
"labor": "",
"rate": 0,
"text": ""
}
]
},
{
"itemCategory": "Kitchen",
"task": "Kitchen",
"children": [
{
"task": "Micro oven",
"price": 200,
"leaf": true,
"labor": "",
"rate": 0,
"text": ""
},
{
"task": "Refrigerator",
"price": 250,
"leaf": true,
"labor": "4",
"rate": 3,
"select": true
}
]
},
{
"itemCategory": "Kitchen",
"task": "Kitchen",
"children": [
{
"task": "Dining Table",
"price": 100,
"leaf": true,
"labor": "",
"rate": 0,
"text": ""
}
]
}
],
[
{
"itemCategory": "Bedroom",
"task": "Bedroom",
"children": [
{
"task": "Smart TV",
"price": 300,
"leaf": true,
"labor": "",
"rate": 0,
"text": ""
},
{
"task": "Bed",
"price": 500,
"leaf": true,
"labor": "",
"rate": 0,
"text": ""
}
]
},
{
"itemCategory": "Kitchen",
"task": "Kitchen",
"children": [
{
"task": "Micro oven",
"price": 200,
"leaf": true,
"labor": "",
"rate": 0,
"text": ""
},
{
"task": "Refrigerator",
"price": 250,
"leaf": true,
"labor": "",
"rate": 0,
"text": ""
}
]
},
{
"itemCategory": "Kitchen",
"task": "Kitchen",
"children": [
{
"task": "Dining Table",
"price": 100,
"leaf": true,
"labor": "5",
"rate": 4,
"text": "",
"select": true
}
]
}
]
]
首先,我在比较项目名称。一旦我得到了,我需要搜索“select:true”
{
"task": "Smart TV",
"price": 300,
"leaf": true,
"labor": "5",
"rate": 4,
"text": "",
"select": true
},
{
"task": "Refrigerator",
"price": 250,
"leaf": true,
"labor": "4",
"rate": 3,
"select": true
},
{
"task": "Dining Table",
"price": 100,
"leaf": true,
"labor": "5",
"rate": 4,
"text": "",
"select": true
}
提前致谢。
答案 0 :(得分:2)
这是没有外部库的方法。为简化嵌套数组的操作,flatMap
已定义。
flatMap -> [[1,2], [3,4]] to [1,2,3,4]
const data = [ [ { itemCategory: 'Bedroom', task: 'Bedroom', children: [ { task: 'Smart TV', price: 300, leaf: true, labor: '', rate: 0, text: '' }, { task: 'Bed', price: 500, leaf: true, labor: '', rate: 0, text: '' } ] }, { itemCategory: 'Kitchen', task: 'Kitchen', children: [ { task: 'Micro oven', price: 200, leaf: true, labor: '', rate: 0, text: '' }, { task: 'Refrigerator', price: 250, leaf: true, labor: '', rate: 0, text: '' } ] }, { itemCategory: 'Kitchen', task: 'Kitchen', children: [ { task: 'Dining Table', price: 100, leaf: true, labor: '', rate: 0, text: '' } ] } ], [ { itemCategory: 'Bedroom', task: 'Bedroom', children: [ { task: 'Smart TV', price: 300, leaf: true, labor: '5', rate: 4, text: '', select: true }, { task: 'Bed', price: 500, leaf: true, labor: '', rate: 0, text: '' } ] }, { itemCategory: 'Kitchen', task: 'Kitchen', children: [ { task: 'Micro oven', price: 200, leaf: true, labor: '', rate: 0, text: '' }, { task: 'Refrigerator', price: 250, leaf: true, labor: '4', rate: 3, select: true } ] }, { itemCategory: 'Kitchen', task: 'Kitchen', children: [ { task: 'Dining Table', price: 100, leaf: true, labor: '', rate: 0, text: '' } ] } ], [ { itemCategory: 'Bedroom', task: 'Bedroom', children: [ { task: 'Smart TV', price: 300, leaf: true, labor: '', rate: 0, text: '' }, { task: 'Bed', price: 500, leaf: true, labor: '', rate: 0, text: '' } ] }, { itemCategory: 'Kitchen', task: 'Kitchen', children: [ { task: 'Micro oven', price: 200, leaf: true, labor: '', rate: 0, text: '' }, { task: 'Refrigerator', price: 250, leaf: true, labor: '', rate: 0, text: '' } ] }, { itemCategory: 'Kitchen', task: 'Kitchen', children: [ { task: 'Dining Table', price: 100, leaf: true, labor: '5', rate: 4, text: '', select: true } ] } ] ]
function flatMap(arr) {
return arr.reduce((acc, cur) => {
acc = acc.concat(cur)
return acc
}, [])
}
const result = flatMap(flatMap(data).map(x => flatMap(x.children))).filter(x => x.select == true)
console.log(result)