Given a nested array of meals, we're trying to remove a nested object based on an id and mealType combo. We start of with 2 nested breakfast meals, if both are removed, we're trying to remove the parent key completely, since it is now empty.
Here is the code we have so far:
var mealsList = { "menuGroup1": [ { "id": "b1", "menuId": "FRPUbgmMiaNH", "title": "Eggs", "mealType": "breakfast" }, { "id": "b2", "menuId": "FRPUbgmMiaNH", "title": "Sandwich", "mealType": "breakfast" } ], "menuGroup2": [ { "id": "b2", "menuId": "FRPUbgmMiaNH", "title": "Fruits", "mealType": "snack" } ] }; console.log(mealsList); const removeItem = (id, mealType) => { // pseudocode // mealsList.*.where("id" == id && "mealType" == mealType).remove(); } // Remove breakfast with id and meal_type // removeItem(id, mealType) removeItem('b1', 'breakfast'); console.log('removed b1 breakfast, mealsList should only contain b2 object'); console.log(mealsList); removeItem('b2', 'breakfast'); console.log('removed b2 breakfast, mealsList should only contain menuGroup2, as menuGroup1 is now empty'); console.log(mealsList); The thing is we're trying to remove the nested meal object in removeItem without explicitly passing menuGroupX, instead, we're trying to target the nested mealsList object based on the id and mealType. The pseudo code is using a * to show that.
The console.log statements show the expected outcome in terms of how the mealsList should render under them.
We're looking for something short and sweet, ideally a one liner, even if that means using a lodash solution for simplicity.
Here is the fiddle (Code In JS tab)
Any idea what to put instead of the pseudocode in removeItem to have the mealsList remove the specific items?