1var colors = ["red","blue","car","green"];
2var carIndex = colors.indexOf("car");//get "car" index
3//remove car from the colors array
4colors.splice(carIndex, 1); // colors = ["red","blue","green"]
1var colors = ["red","blue","car","green"];
2var carIndex = colors.indexOf("car");//get "car" index
3//remove car from the colors array
4colors.splice(carIndex, 1); // colors = ["red","blue","green"]
1let value = 3
2
3let arr = [1, 2, 3, 4, 5, 3]
4
5arr = arr.filter(item => item !== value)
6
7console.log(arr)
8// [ 1, 2, 4, 5 ]
1var myArray = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
2
3//removing element using splice method --
4//arr.splice(index of the item to be removed, number of elements to be removed)
5//Here lets remove Sunday -- index 0 and Monday -- index 1
6 myArray.splice(0,2)
7
8//using filter method
9let itemToBeRemoved = ["Sunday", "Monday"]
10var filteredArray = myArray.filter(item => !itemToBeRemoved.includes(item))
1let originalArray = [1, 2, 3, 4, 5];
2
3let filteredArray = originalArray.filter((value, index) => index !== 2);
1// - - - - - - - - - - -
2// Remove Last Element (pop)
3// - - - - - - - - - - -
4// example (remove the last element in the array)
5let yourArray = ["aaa", "bbb", "ccc", "ddd"];
6yourArray.pop(); // yourArray = ["aaa", "bbb", "ccc"]
7
8// syntax:
9// <array-name>.pop();
10
11// - - - - - - - - - - -
12// Remove First Element (shift)
13// - - - - - - - - - - -
14// example (remove the last element in the array)
15let yourArray = ["aaa", "bbb", "ccc", "ddd"];
16yourArray.shift(); // yourArray = ["bbb", "ccc", "ddd"]
17
18// syntax:
19// <array-name>.shift();