js remove multiple items from array code example
Example 1: javascript removing items looping through array
var colors=["red","green","blue","yellow"];
for (var i = colors.length - 1; i >= 0; i--) {
if (colors[i] === "green" || colors[i] === "blue") {
colors.splice(i, 1);
}
}
Example 2: remove multiple values from array javascript
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));
}
Example 3: javascript to remove few items from array
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));
}
console.log(removeFromArray(nums, remove));
Example 4: js remove several elements from array
var ar = [1, 2, 3, 4, 5, 6];ar.pop();