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))
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
1let forDeletion = [2, 3, 5]
2
3let arr = [1, 2, 3, 4, 5, 3]
4
5arr = arr.filter(item => !forDeletion.includes(item))
6// !!! Read below about array.includes(...) support !!!
7
8console.log(arr)
9// [ 1, 4 ]