remove array by index code example
Example 1: javascript remove from array by index
array.splice(index, 1);
Example 2: 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 3: remove item at index in array javascript
let arr = [0,1,2,3,4,5]
let newArr = [...arr]
newArr.splice(1,1)
console.log(arr)
console.log(newArr)
Example 4: remove one element using splice
fruits = ['Banana', 'Orange', 'Apple', 'Mango'];
removeFruitByIndex(index: number) {
this.fruits = [
...this.fruits.slice(0, i),
...this.fruits.slice(i + 1, this.fruits.length),
];
}
removeFruitByValue(fruite: string) {
const i = this.descriptionsList.indexOf(fruite);
this.fruits = [
...this.fruits.slice(0, i),
...this.fruits.slice(i + 1, this.fruits.length),
];
}
Example 5: javascript array clear
var cars = ["mazda","honda","tesla"];
cars = [];
Example 6: javascript remove index from array
array.splice(index, 1);