delete array javascript by index code example
Example 1: remove element from array in js
var myArray = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
myArray.splice(0,2)
let itemToBeRemoved = ["Sunday", "Monday"]
var filteredArray = myArray.filter(item => !itemToBeRemoved.includes(item))
Example 2: 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 3: remove elemtns from an array with splice
var fruits = ["Banana", "Orange", "Apple", "Mango", "Kiwi"];
document.getElementById("demo").innerHTML = fruits;
function myFunction() {
fruits.splice(2, 2);
document.getElementById("demo").innerHTML = fruits;
}
Example 4: how to delete an element from an array in javascript
["bar", "baz", "foo", "qux"]list.splice(0, 2)