Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove object from array based on array of some property of that object

I have an array of objects (objList) that each has "id" property.

I have an array of strings (idsToRemove), representing IDs of the objects to remove from objList.

I find some solution but I fear it's slow, especially with the large list of objects with lots of properties. Is there more efficient way to do this?

var idsToRemove = ["3", "1"];
var objList = [{
    id: "1",
    name: "aaa"
  },
  {
    id: "2",
    name: "bbb"
  },
  {
    id: "3",
    name: "ccc"
  }
];

for (var i = 0, len = idsToRemove.length; i < len; i++) {
  objList = objList.filter(o => o.id != idsToRemove[i]);
}

console.log(objList);
like image 999
Dalibor Avatar asked Mar 07 '19 10:03

Dalibor


People also ask

How do you remove an element from an array based on value?

We can use the following JavaScript methods to remove an array element by its value. indexOf() – function is used to find array index number of given value. Return negavie number if the matching element not found. splice() function is used to delete a particular index value and return updated array.

How do you remove a property property from an object obj?

Answer: Use the delete Operator You can use the delete operator to completely remove the properties from the JavaScript object. Deleting is the only way to actually remove a property from an object.

How do I remove an object from an array by id?

The Array findIndex() and splice() Methods To remove an element from an array by ID in JavaScript, use the findIndex() method to find the index of the object with the ID in the array. Then call the splice() method on the array, passing this index and 1 as arguments to remove the object from the array.


2 Answers

Turn the idsToRemove into a Set so that you can use Set.prototype.has (an O(1) operation), and .filter the objList just once, so that the overall complexity is O(n) (and you only iterate over the possibly-huge objList once):

var idsToRemove = ["3", "1"];
var objList = [{
    id: "1",
    name: "aaa"
  },
  {
    id: "2",
    name: "bbb"
  },
  {
    id: "3",
    name: "ccc"
  }
];

const set = new Set(idsToRemove);
const filtered = objList.filter(({ id }) => !set.has(id));
console.log(filtered);

Note that Array.prototype.includes and Array.prototype.indexOf operations are O(N), not O(1), so if you use them instead of a Set, they may take significantly longer.

like image 127
CertainPerformance Avatar answered Sep 30 '22 07:09

CertainPerformance


You can use Array.includes which check if the given string exists in the given array and combine it with an Array.filter.

const idsToRemove = ['3', '1'];

const objList = [{
    id: '1',
    name: 'aaa',
  },
  {
    id: '2',
    name: 'bbb',
  },
  {
    id: '3',
    name: 'ccc',
  },
];

const filteredObjList = objList.filter(x => !idsToRemove.includes(x.id));

console.log(filteredObjList);
like image 4
Orelsanpls Avatar answered Sep 30 '22 09:09

Orelsanpls