下面是我的JSON對象,我想從中刪除groupType
,因爲它沒有trip.ie。,trips.length===0
。並且如果全部刪除整個客戶端對象車次數組爲空爲該特定客戶對象..根據條件刪除嵌套JSON中的元素
let fruitsArray= [
{
"fruit": {
"id": 1,
"name": "Mango"
},
"group": [
{
"groupType": {
"id": 1,
"name": "A"
},
"trips": []
},
{
"groupType": {
"id": 2,
"name": "B"
},
"trips": []
},
{
"groupType": {
"id": 3,
"name": "C"
},
"trips": []
},
{
"groupType": {
"id": 4,
"name": "D"
},
"trips": []
}
]
},
{
"fruit": {
"id": 1,
"name": "Apple"
},
"group": [
{
"groupType": {
"id": 1,
"name": "A"
},
"trips": []
},
{
"groupType": {
"id": 2,
"name": "B"
},
"trips": [
{
"trip": {
"id": 62,
"deliverySlotId": 900000000000001
}
}
]
},
{
"groupType": {
"id": 3,
"name": "C"
},
"trips": []
},
{
"groupType": {
"id": 4,
"name": "D"
},
"trips": []
}
]
},
{
"fruit": {
"id": 95,
"name": "Banana"
},
"group": [
{
"groupType": {
"id": 4,
"name": "A1"
},
"trips": [
{
"trip": {
"id": 62,
"deliverySlotId": 900000000000001
}
},
{
"trip": {
"id": 63,
"deliverySlotId": 900000000000001
}
},
{
"trip": {
"id": 65,
"deliverySlotId": 900000000000001,
"orderId": 22
}
}
]
}
]
}
]
,我試圖返回0 groupType
所有元素的代碼..
let finalArray = fruitsArray.map((group) => {
return group.group.filter((trip) => {
return trip.trips.length > 0;
})
})
你也想如果沒有在裏面游來刪除整個水果? – JuHwon
是@ JuHwon.The整個水果 – forgottofly
而不是地圖,你可以使用減少和有條件地推動對象,如果該組沒有旅行 'let finalArray = fruitsAray.reduce((res,group)=> { return res.concat (group.group.filter((trip)=> { return trip.trips.length> 0; })|| []); },[])' 我還沒有測試過代碼。我希望你明白這個主意。 – z0mBi3