array element remove code example
Example 1: remove a particular element from array
var colors = ["red","blue","car","green"];
var carIndex = colors.indexOf("car");
colors.splice(carIndex, 1);
Example 2: remove item from array javascript
const array = [2, 5, 9];
console.log(array);
const index = array.indexOf(5);
if (index > -1) {
array.splice(index, 1);
}
console.log(array);
Example 3: delete item from list javascript
const array = [1, 2, 3];
const index = array.indexOf(2);
if (index > -1) {
array.splice(index, 1);
}
Example 4: remove an element from array
var colors = ["red", "blue", "car","green"];
colors = colors.filter(data => data != "car");
colors = colors.filter(function(data) { return data != "car"});
Example 5: how to delete an element of an array in javascript
let animals1 = ["dog", "cat", "mouse"]
delete animals1[1]
console.log(animals1)
let animals2 = ["dog", "cat", "mouse"]
animals2.splice(1, 1)
console.log(animals2)
let animals3 = ["dog", "cat", "mouse"]
animals3.splice(0, 2)
console.log(animals3)
Example 6: javascript array clear
var cars = ["mazda","honda","tesla"];
cars = [];