Answers for "javascript remove multiple items from array"

11

javascript remove multiple items from array

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"]
Posted by: Guest on August-02-2019
0

remove multiple elements from array es6

const colors=["red","green","blue","yellow"];
let index = colors.length - 1;
while (index >= 0) {
  	if (['green', 'blue'].indexOf(colors[index]) > (-1)) {
    	colors.splice(index, 1);
  	}
  	index -= 1;
}
Posted by: Guest on July-05-2021
0

js remove several elements from array

var array = [1, 2, 3, 4];var evens = _.remove(array, function(n) { return n % 2 === 0;});console.log(array);// => [1, 3]console.log(evens);// => [2, 4]
Posted by: Guest on July-10-2020
0

js remove several elements from array

var ar = [1, 2, 3, 4, 5, 6];ar.length = 4; // set length to remove elementsconsole.log( ar ); // [1, 2, 3, 4]
Posted by: Guest on July-10-2020
0

js remove several elements from array

var ar = [1, 2, 3, 4, 5, 6];ar.pop(); // returns 6console.log( ar ); // [1, 2, 3, 4, 5]
Posted by: Guest on July-10-2020
-1

js remove several elements from array

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]
Posted by: Guest on July-10-2020

Code answers related to "javascript remove multiple items from array"

Code answers related to "Javascript"

Browse Popular Code Answers by Language