-1

I have a main array of objects with each object having some key/values as well as a "id" key with 1,2,3,4,5, etc Now I have another array representing just id's (like [2,3])

I want to use this array to delete objects from the main array...so in this case, objects from the main array having id's 2 & 3 should be deleted

While I am aware of findBy(id), I am not sure if that can be used to delete multiple objects at once.

testndtv
  • 43,898
  • 91
  • 265
  • 396
  • run a loop on the second array and then delete then matching elements from the first array – Alien Jun 18 '20 at 10:14
  • Do you want to **delete** items from the original array or to get a new filtered one? – briosheje Jun 18 '20 at 10:19
  • You haven't found anything that would give a direction on where to go? Like [Remove all elements contained in another array](https://stackoverflow.com/questions/19957348/remove-all-elements-contained-in-another-array) or [javascript find and remove object in array based on key value](https://stackoverflow.com/questions/21659888/javascript-find-and-remove-object-in-array-based-on-key-value) or ... – Andreas Jun 18 '20 at 10:22
  • Does this answer your question? [Remove array element based on object property](https://stackoverflow.com/questions/15287865/remove-array-element-based-on-object-property) – Rohit Ambre Jun 18 '20 at 10:24

7 Answers7

3

You can use filter. In the filter callback function check if the id is also there in id array by using includes

let idArr = [1, 2]
let obj = [{
    id: 1,
    name: 'abc'
  },
  {
    id: 2,
    name: 'abc'
  },
  {
    id: 3,
    name: 'abc'
  },
  {
    id: 4,
    name: 'abc'
  }
];

let data = obj.filter(item => !idArr.includes(item.id));
console.log(data);
console.log(obj)
brk
  • 43,022
  • 4
  • 37
  • 61
1

using filter might work well here. you could write something like:

var newArray = oldArray.filter(object => !ids.includes(object.id))
Ewan Sheldon
  • 31
  • 1
  • 2
0

You can do it, like this:

[2,3].forEach(key => {
    delete object[key];
})
mgm793
  • 694
  • 6
  • 17
0

You can use filter method for this. Ex:

let id = 2;
let list = [{
  Id: 1,
  Name: 'a'
}, {
  Id: 2,
  Name: 'b'
}, {
  Id: 3,
  Name: 'c'
}];
let lists = list.filter(x => {
  return x.Id != id;
})
console.log(lists);
Pramod
  • 26
  • 5
0

Assuming you want to delete items from the original array by entirely removing the element from the array (and you don't want to get a new array), you can take advantage of Array.splice

let idArr = [1, 2];
let obj = [{
    id: 1
  },
  {
    id: 2
  },
  {
    id: 3
  },
  {
    id: 4
  }
];

for (let id of idArr) {
  // look for the element by its id.
  const objIdRef = obj.find(i => i.id === id);
  // if it actually exists, splice it.
  objIdRef && obj.splice(obj.indexOf(objIdRef), 1);
}
console.log(obj);

If the obj array is big, you might want to make a map from it before processing the id array, so that the complexing is reduced to O(1) when the delete process begins.

briosheje
  • 6,538
  • 2
  • 31
  • 48
0

Perhaps This is what you want:

var arr= [{id:1, name: "foo"}, {id:2, name: "bar"}, {id:3, name:"not to be deleted"}];

var idsToDelete = [1, 2];

var res = arr.map((i, idx)=>{
   return arr[idx] = idsToDelete.includes(i.id)? undefined : arr[idx]
}).filter(i=>i)

console.log(res)
ABGR
  • 3,606
  • 1
  • 15
  • 35
0

You can try Lodash.js functions _.forEach() and _.remove()

let valuesArr = [
    {id: 1, name: "dog"}, 
    {id: 2, name: "cat"}, 
    {id: 3, name: "rat"}, 
    {id: 4, name: "bat"},
    {id: 5, name: "pig"},
]; 
let removeValFromIndex = [
    {id: 2, name: "cat"}, 
    {id: 5, name: "pig"},
]; 
_.forEach(removeValFromIndex, (indi) => {
    _.remove(valuesArr, (item) => {
        return item.id === indi.id;
    });
})

console.log(valuesArr)
/*[
    {id: 1, name: "dog"},  
    {id: 3, name: "rat"}, 
    {id: 4, name: "bat"},
]; */

Don't forget to clone (_.clone(valuesArr) or [...valuesArr]) before mutate your array

Egils
  • 1
  • 2