forgottofly forgottofly - 4 months ago 8
JSON Question

Remove elements inside nested JSON based on condition

Below is my JSON object in which I want to remove the

groupType
from the group that dosen't have trips.ie.,
trips.length===0
.And also want to remove the entire client object if all the trips array is empty for that particular client object..

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
}
}
]
}
]
}
]


The code that I have tried returns all the elements with 0
groupType
..

let finalArray = fruitsArray.map((group) => {
return group.group.filter((trip) => {
return trip.trips.length > 0;
})
})

Answer

You can use map combined with filter. Alternatively you can also use the reduce method.

I wrote both examples beneath.

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);