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"]
1var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
2var filtered = array.filter(function(value, index, arr){
3 return value > 5;
4});
5//filtered => [6, 7, 8, 9]
6//array => [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
1// remove element at certain index without changing original
2let arr = [0,1,2,3,4,5]
3let newArr = [...arr]
4newArr.splice(1,1)//remove 1 element from index 1
5console.log(arr) // [0,1,2,3,4,5]
6console.log(newArr)// [0,2,3,4,5]
1//you can use two functions depending of what you want to do:
2
3let animals1 = ["dog", "cat", "mouse"]
4delete animals1[1]
5/*this deletes all the information inside "cat" but the element still exists
6so now you'll have this:*/
7console.log(animals1)//animals1 = ["dog", undefined, "mouse"]
8
9//if you want to delete it completely, you have to use array.splice:
10
11let animals2 = ["dog", "cat", "mouse"]
12animals2.splice(1, 1)
13/*the first number means the position from which you want to start to delete
14and the second is how much elements will be deleted*/
15console.log(animals2)//animals2 = ["dog", "mouse"]
16/*Now you don't have undefined
17If you did this:*/
18let animals3 = ["dog", "cat", "mouse"]
19animals3.splice(0, 2)//you'll have this:
20console.log(animals3)//animals 3 = "mouse"
21/*This happens because I put a 2 in the second parameter so it deleted
22two elements from position 0
23Try copying this code in your console and whatch*/