0

I am trying to delete the all the objects with _id "bennyRawMaterial" from below details array nested in the object:

let recipeBasicRecipes = [{
_id:'12345',
name:'Macaron Shell',
details:[
  {
    name: 'Sugar',
    _id: 'bennyRawMaterial',
    type: 'solid',
  }
,  
  {  
    name: 'Egg white',
    _id: '5fef680ca43301322a3224e5',
    type: 'solid'
  }]
},
{
_id:'14512345',
name:'Macaron Shell',
details:[{  
    name: 'Castiors gar',
    _id: 'bennyRawMaterial',
    type: 'solid'
  },
  {
    name: 'oil',
    _id: 'bennyRawMaterial',
    type: 'solid',
  }
,  {
    name: 'food',
    _id: 'bennyRawMaterial',
    type: 'solid',

  }]
}]

I am using following code to remove the objects, but it skips few of the objects.Please help with the implementation

recipeBasicRecipes.forEach(br => {
        br.details.forEach((rm, index) => {
          if (rm._id === 'bennyRawMaterial') {
            br.details.splice(index, 1);
          } else {
            return true;
          }
        });

sgrmhdk
  • 230
  • 1
  • 8
  • 1
    What's the expected output here? Do you want to remove all the objects with id `bennyRawMaterial` or only the duplicate ones and keep atleast one? – rahulpsd18 Jan 25 '21 at 09:06
  • Hi I want to delete all the object with id bennyRawMaterial – sgrmhdk Jan 25 '21 at 09:07
  • Use a regular for loop (or a while loop like shown in the dupe), and loop backwards through the indexes of your array when using `.splice()` – Nick Parsons Jan 25 '21 at 09:09
  • 1
    `recipeBasicRecipes.forEach(br => { br.details = br.details.filter(x => x._id != 'bennyRawMaterial'); });` should work here.. I am filtering out all the objects with id set to "bennyRawMaterial" here. – rahulpsd18 Jan 25 '21 at 09:11

1 Answers1

1
  • maintain a global ids array,
  • and go through the details array of each object
  • check the id exist in global ids array

let recipeBasicRecipes = [{
    _id: '12345',
    name: 'Macaron Shell',
    details: [{
        name: 'Sugar',
        _id: 'bennyRawMaterial',
        type: 'solid',
      },
      {
        name: 'Egg white',
        _id: '5fef680ca43301322a3224e5',
        type: 'solid'
      }
    ]
  },
  {
    _id: '14512345',
    name: 'Macaron Shell',
    details: [{
        name: 'Castiors gar',
        _id: 'bennyRawMaterial',
        type: 'solid'
      },
      {
        name: 'oil',
        _id: 'bennyRawMaterial',
        type: 'solid',
      }, {
        name: 'food',
        _id: 'bennyRawMaterial',
        type: 'solid',

      }
    ]
  }
]


var uniqueIds = []; //global ids array
recipeBasicRecipes.forEach(el => { 

  
  let details = [];//unique details array
  el.details.forEach((dt, i) => {

    let id = dt._id;
    if (!uniqueIds.includes(id)){ //check id exists in global ids array
      uniqueIds.push(id);
      details.push(dt); //copy unique details
    }
  });

  el.details = details; //update details with unique details
});

console.log(recipeBasicRecipes)
Azad
  • 5,144
  • 4
  • 28
  • 56