0

When I want to remove one element, it is easy. This is my function:

function removeValues(array, value) {
    for(var i=0; i<array.length; i++) {
        if(array[i] == value) {
            array.splice(i, 1);
            break;
        }
    }
    return array;
}

But how do I remove multiple elements?

Marcos Dimitrio
  • 5,592
  • 3
  • 33
  • 56
Valeriu
  • 161
  • 1
  • 9
  • 2
    What multiple elements? Please explain with an example? – thefourtheye Nov 08 '15 at 16:58
  • Possible duplicate of [How to remove a particular elements from an array in javascript?](http://stackoverflow.com/questions/33595155/how-to-remove-a-particular-elements-from-an-array-in-javascript) – melpomene Nov 08 '15 at 17:00
  • 3
    Call `removeValues` for each value? – Felix Kling Nov 08 '15 at 17:01
  • 2
    Possible duplicate of [Javascript arrays: remove all elements contained in another array](http://stackoverflow.com/questions/19957348/javascript-arrays-remove-all-elements-contained-in-another-array) – Marcos Dimitrio Nov 08 '15 at 17:04

3 Answers3

12

Here a simple version using ES7:

// removing values
let items = [1, 2, 3, 4];
let valuesToRemove = [1, 3, 4]
items = items.filter((i) => !valuesToRemove.includes(i))

For a simple version for ES6

// removing values
let items =[1, 2, 3, 4];
let valuesToRemove = [1, 3, 4]
items = items.filter((i) => (valuesToRemove.indexOf(i) === -1))
leonheess
  • 5,825
  • 6
  • 42
  • 67
JDCL32
  • 186
  • 1
  • 5
0
const items = [0, 1, 2, 3, 4];

[1, 4, 3].reverse().forEach((index) => {
    items.splice(index, 1)
})

// [0, 2, 4]
  • 1
    The community encourages adding explanations alongisde code, rather than purely code-based answers (see [here](https://meta.stackoverflow.com/questions/300837/what-comment-should-i-add-to-code-only-answers)). – costaparas Feb 06 '21 at 03:10
-2

I believe you will find the kind of functionality you are looking for in Javascript's built in array functions... particularily Array.map(); and Array.filter();

//Array Filter
function isBigEnough(value) {
  return value >= 10;
}
var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
// filtered is [12, 130, 44]


//Array Map (Can also be used to filter)
var numbers = [1, 4, 9];
var doubles = numbers.map(function(num) {
  return num * 2;
});
// doubles is now [2, 8, 18]. numbers is still [1, 4, 9]

/////UPDATE REFLECTING REMOVAL OF VALUES USING ARRAY MAP

var a = [1,2,3,4,5,6];
a.map(function(v,i){
  if(v%2==0){
 a.pop(i);
  }
});
console.log(a);

// as shown above all array functions can be used within the call back to filter the original array. Alternativelty another array could be populated within the function and then aassigned to the variable a effectivley reducing the array.
kurt
  • 1,106
  • 1
  • 7
  • 18
  • What exactly has `.map` to do with this? – Felix Kling Nov 08 '15 at 17:03
  • give an example so the OP can see what you mean. – Felipe The Mentor Nov 08 '15 at 17:04
  • [QUOTE] Summary The map() method creates a new array with the results of calling a provided function on every element in this array. – kurt Nov 08 '15 at 17:04
  • 3
    Right. So how would you use it to remove an element? – Felix Kling Nov 08 '15 at 17:05
  • I usually use map on multidimensional arrays when I need to get odd data out. I simply push the values to a new array that I return. It might not be the best way but its not the worst either. – kurt Nov 08 '15 at 17:07
  • 1
    @kurt i upvoted your answer at first, but after you updated the answer with an example i am forced to downvote it as it does not answer op's and felix's question. op needs to remove elements from an array, not alter their value. – Banana Nov 08 '15 at 17:09
  • 1
    *"I simply push the values to a new array that I return."* So you are not actually removing elements from the array you call `.map` on. This could be a good answer if you didn't try to include `.map`. – Felix Kling Nov 08 '15 at 17:15
  • I see you point. Technically im returning a modified clone. But you can still use it to remove values from the array. see my example http://jsfiddle.net/pnL0awcv/ – kurt Nov 08 '15 at 17:26
  • @Banana I have updated the code example further to show that Array.map(); can be used to modify the array it was called on. – kurt Nov 08 '15 at 17:36
  • @FelixKling please see updated example removing values from an array using Array.map – kurt Nov 08 '15 at 17:37
  • That's not the correct use of `.map`. If you are not actually returning anything from the callback to create a new array, i.e. you just want to iterate over the array, use [**`.forEach`**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach). Again, using `.filter` is great. `.map` has no place here. – Felix Kling Nov 08 '15 at 20:31
  • `pop` does not take an argument. –  Feb 10 '17 at 17:58