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];
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))
1var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
2var removed = arr.splice(2,2);
3/*
4removed === [3, 4]
5arr === [1, 2, 5, 6, 7, 8, 9, 0]
6*/
1const array = [2, 5, 9];
2
3console.log(array);
4
5const index = array.indexOf(5);
6if (index > -1) {
7 array.splice(index, 1);
8}
9// array = [2, 9]
10console.log(array);
1/* there are 3 ways to do so
21) pop(), which removes the last element
32) shift(), which removes the first element
43) splice(), which removes any element with a specified index.
5of these only splice() takes parameters. the first parameter it takes is the
6index of the element to be removed, an integer. the second is the number of
7elements to be removed, again integer. the third and fourth etc. are optional,
8which is to be used if you want to replace them. Example: */
9let numbers = [1, 2, 3, 4, 5];
10numbers.pop(); // removes 5
11numbers.shift(); // removes 1
12let threeIndex = numbers.indexOf(3); // used for long arrays
13numbers.splice(threeIndex, 1); // without replacement
14numbers.splice(threeIndex, 1, 7) // 3 will now be replaced by 7