Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter and delete filtered elements in an array

I want to remove specific elements in the original array (which is var a). I filter() that array and splice() returned new array. but that doesn't affect the original array in this code. How can I easily remove those elements from the original array?

var a = [{name:'tc_001'}, {name:'tc_002'}, {name:'tc_003'}]  var b = a.filter(function (e) {     return e.name === 'tc_001'; });  b.splice(0,1);  console.log(a); console.log(b); 
like image 406
s1n7ax Avatar asked May 23 '16 07:05

s1n7ax


People also ask

How do you remove an element from an array filter?

pop - Removes from the End of an Array. shift - Removes from the beginning of an Array. splice - removes from a specific Array index. filter - allows you to programatically remove elements from an Array.

How do I remove a specific element from an array?

pop() function: This method is use to remove elements from the end of an array. shift() function: This method is use to remove elements from the start of an array. splice() function: This method is use to remove elements from the specific index of an array.

How do you filter an element in an array?

JavaScript Array filter()The filter() method creates a new array filled with elements that pass a test provided by a function. The filter() method does not execute the function for empty elements. The filter() method does not change the original array.

Which is method syntax correct to remove item from array?

array. remove(index) or array.


2 Answers

The Array.prototype.filter() method is used to collect an element set not only one item. If you would like to get one item by evaluating a condition then you have three other options:

  • Array.prototype.indexOf()
  • Array.prototype.findIndex()
  • Array.prototype.find()

Accordingly only if you want to make an operation on more than one item you should think of using the filter function. None of the answers is complete in terms of the job that is needed to be done.

They use the filter function to isolate a set (happens to be only one item in this example) but they don't show how to get rid of the whole set. Well ok, let's clarify.

If you want to do find and delete only one item of your array it shall be done like this

var a = [{name:'tc_001'}, {name:'tc_002'}, {name:'tc_003'}]; a.splice(a.findIndex(e => e.name === "tc_001"),1); console.log(a);

However since you mention "specific elements" in plural, then you will need to collect a set of selected items and do the above job one by one on each element in the set. So the proper approach would be.

var a = [{name:'tc_001'}, {name:'tc_002'}, {name:'tc_003'}],     b = a.filter(e => e.name === "tc_001"); b.forEach(f => a.splice(a.findIndex(e => e.name === f.name),1)); console.log(a);

Regardless of how many elements there are in your selected list, this will do your job. Yet I believe although this looks logical it does tons of redundant job. First filters and then per filtered element does index search this and that. Although I know that findIndex is crazy fast still I would expect this one to turn out to be noticeably slow especially with big arrays. Let's find an O(n) solution. Here you go:

var a = [{name:'tc_001'}, {name:'tc_002'}, {name:'tc_003'}]; a = a.reduce((p,c) => (c.name !== "tc_001" && p.push(c),p),[]); console.log(a);

So this must be it.

like image 189
Redu Avatar answered Oct 13 '22 00:10

Redu


Another way is filtering in two list like this:

const originalList = [{condition:true}, {condition: false}, {condition: true}];  // wished lists const listWithTrue = originalList.filter(x=>x.condition); const listWithFalse = originalList.filter(x=>!x.condition); // inverse condition 
like image 34
Ninja Coding Avatar answered Oct 13 '22 00:10

Ninja Coding