简体   繁体   中英

How to remove elements from an array using filter() in JavaScript?

I need to remove all the elements from an array , and all the elements that are to be removed are being passed as arguments. I am trying to use the filter method to remove them but it is not working properly. The code is :

 function destroyer(arr) { // Remove all the values var args = Array.from(arguments); args.shift(); console.log(args); var arr1 = arr.filter(function(v){ for(var i= 0;i < args.length;i++){ if(arr.indexOf(args[i]) !== -1) return false; } }); console.log(arr1); return arr; } destroyer([1, 2, 3, 1, 2, 3], 2, 3); 

I am using args array to store all the arguments into array.The first argument is the array from which elements are to be removed and the following arguments are the ones that are to deleted(It is sure they are present in the array) but somehow the filter function is not working properly and also for more info see, FreeCodeCamp challenge

You need to return arr1 instead of arr .

The filter() method creates a new array instead of modifying the source array, so you need to return the newly created array from your method.

As to your filtering logic, I believe it should look something like this:

 function destroyer(arr) { let args = Array.from(arguments); args.shift(); return arr.filter(v => args.indexOf(v) === -1); } console.log(destroyer([1, 2, 3, 1, 2, 3], 2, 3)); 

Or if you're only targeting browsers that already support rest parameters :

 function destroyer(arr, ...args) { return arr.filter(v => args.indexOf(v) === -1); } console.log(destroyer([1, 2, 3, 1, 2, 3], 2, 3)); 

you need to change your filter function, check if each value in arr is in args , if yes return false for it otherwise return true .

 function destroyer(arr) { // Remove all the values var args = Array.from(arguments); args.shift(); console.log(args); var arr1 = arr.filter(function(v){ return (args.indexOf(v) !== -1) ? false : true; }); console.log(arr1); return arr; } destroyer([1, 2, 3, 1, 2, 3], 2, 3); 

Just for the record, the problem with your code is that you're looping over all elements of args every time, so you return false if any element of arr matches any element of args (the actual value passed to the callback is ignored).

To fix your code, get rid of that loop and test v against args :

 function destroyer(arr) { // Remove all the values var args = Array.from(arguments); args.shift(); console.log(args); var arr1 = arr.filter(function(v) { // for (var i=0; i < args.length; i++) { if (args.indexOf(v) !== -1) { return false; } return true; // } }); console.log(arr1); return arr1; // return correct array } destroyer([1, 2, 3, 1, 2, 3], 2, 3); 

Once you've done that, it's simple to see how it can be reduced (using rest parameters and an arrow function) to:

 function destroyer(arr, ...args) { return arr.filter(v => !args.includes(v)); } console.log( destroyer([1, 2, 3, 1, 2, 3], 2, 3) ); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM