remove item from array at specific index javascript 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: delete item from list javascript
const array = [1, 2, 3];
const index = array.indexOf(2);
if (index > -1) {
array.splice(index, 1);
}
Example 3: How can I remove a specific item from an array
const array = [2, 5, 9];
console.log(array);
const index = array.indexOf(5);
if (index > -1) {
array.splice(index, 1);
}
console.log(array);
Example 4: js remove if
ar = [1, 2, 3, 4];
ar = ar.filter(item => !(item > 3));
console.log(ar)
Example 5: js array delete specific element
var ar = [1, 2, 3, 4, 5, 6];
ar.pop();
console.log( ar );
Example 6: js array delete specific element
var ar = [1, 2, 3, 4, 5, 6];
ar.length = 4;
console.log( ar );