下面是我的JSON对象,我想在其中删除没有trip.ie。,groupType
的组中的trips.length===0
。并且还要删除整个客户端对象对于该特定客户端对象,trips数组为空。
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;
})
})
答案 0 :(得分:1)
您可以将map与filter结合使用。 或者,您也可以使用reduce方法。
我在下面写了两个例子。
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
}
}
]
}
]
}
];
var filteredFruits = fruitsArray.map((fruit) => {
fruit.group = fruit.group.filter((group) => group.trips.length);
return fruit;
}).filter((fruit) => fruit.group.length);
console.log(filteredFruits);
// alternative example with reduce
var reducedFruits = fruitsArray.reduce((prev, next) => {
next.group = next.group.filter((group) => group.trips.length);
return next.group.length ? prev.concat([next]) : prev;
}, []);
console.log(reducedFruits);
答案 1 :(得分:1)
var 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
}
}
]
}
]
}
]
for(var i=0;i<fruitsArray.length;i++){
var currentFruit = fruitsArray[i];
var currentFruitGroups = currentFruit.group;
var foundTrip =false;
for(var j=0;j<currentFruitGroups.length;j++){
var currentTrips = currentFruitGroups[j].trips;
if(!currentTrips.length){
currentFruitGroups.splice(j,1);
j--;
}else{
foundTrip = true;
}
}
if(!foundTrip){
fruitsArray.splice(i,1);
i--;
}
}
console.log(fruitsArray);
答案 2 :(得分:1)
let finalArray = fruitsArray.reduce((p, n) => {
const gt = n.group.filter(g => g.trips.length);
return gt.length ? p.concat({fruit: n.fruit, group: gt}) : p;
}, []);