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 data = [1, 2, 3];
2
3// remove a specific value
4// splice(starting index, how many values to remove);
5data = data.splice(1, 1);
6// data = [1, 3];
7
8// remove last element
9data = data.pop();
10// data = [1, 2];
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 ]
1const array = [2, 5, 9];
2
3//Get index of the number 5
4const index = array.indexOf(5);
5//Only splice if the index exists
6if (index > -1) {
7 //Splice the array
8 array.splice(index, 1);
9}
10
11//array = [2, 9]
12console.log(array);
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();