Tim
0
Q:

remove multiple values from array javascript

var colors=["red","green","blue","yellow"];
//loop back-words through array when removing items like so:
for (var i = colors.length - 1; i >= 0; i--) {
    if (colors[i] === "green" || colors[i] === "blue") { 
        colors.splice(i, 1);
    }
}
//colors is now  ["red", "yellow"]
5
const nums = [1, 2, 3, 4, 5, 6];
const remove = [1, 2, 4, 6];

function removeFromArray(original, remove) {
  return original.filter(value => !remove.includes(value));
}
2
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];for( var i = 0; i < arr.length; i++){ if ( arr[i] === 5) { arr.splice(i, 1); }}//=> [1, 2, 3, 4, 6, 7, 8, 9, 0]
1
var ar = [1, 2, 3, 4, 5, 6];ar.pop(); // returns 6console.log( ar ); // [1, 2, 3, 4, 5]
0
var ar = [1, 2, 3, 4, 5, 6];ar.length = 4; // set length to remove elementsconsole.log( ar ); // [1, 2, 3, 4]
0
var arr1 = [1, 2, 3, 4, 5, 6];var arr2 = arr1; // Reference arr1 by another variable arr1 = [];console.log(arr2); // Output [1, 2, 3, 4, 5, 6]
0

New to Communities?

Join the community