js array delete element by index code example
Example 1: javascript remove from array by index
array.splice(index, 1);
Example 2: how to take an element out of an array in javascript
var data = [1, 2, 3];
data = data.splice(1, 1);
data = data.pop();
Example 3: array remove index from array
const array = [2, 5, 9];
const index = array.indexOf(5);
if (index > -1) {
array.splice(index, 1);
}
console.log(array);
Example 4: delete item from list javascript
const array = [1, 2, 3];
const index = array.indexOf(2);
if (index > -1) {
array.splice(index, 1);
}
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: pop up element from specific index in array
let items = ['a', 'b', 'c', 'd'];
let index = items.indexOf('a')
let numberOfElementToRemove = 1;
if (index !== -1) { items.splice(index,numberOfElementToRemove)}