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];
1// Remove single item
2function removeItemOnce(arr, value) {
3 var index = arr.indexOf(value);
4 if (index > -1) {
5 arr.splice(index, 1);
6 }
7 return arr;
8}
9
10// Remove all items
11function removeItemAll(arr, value) {
12 var i = 0;
13 while (i < arr.length) {
14 if (arr[i] === value) {
15 arr.splice(i, 1);
16 } else {
17 ++i;
18 }
19 }
20 return arr;
21}
22
23// Usage
24console.log(removeItemOnce([2, 5, 9, 1, 5, 8, 5], 5));
25console.log(removeItemAll([2, 5, 9, 1, 5, 8, 5], 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))
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
10// array = [2, 9]
11console.log(array);
1var ar = [1, 2, 3, 4, 5, 6];ar.length = 4; // set length to remove elementsconsole.log( ar ); // [1, 2, 3, 4]
2// //////////////////////////////////////
3var ar = [1, 2, 3, 4, 5, 6];ar.pop(); // returns 6console.log( ar ); // [1, 2, 3, 4, 5]
4// //////////////////////////////////////
5var ar = ['zero', 'one', 'two', 'three'];ar.shift(); // returns "zero"console.log( ar ); // ["one", "two", "three"]
6// //////////////////////////////////////
7var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];var removed = arr.splice(2,2);
8// //////////////////////////////////////
9["bar", "baz", "foo", "qux"]list.splice(0, 2) // Starting at index position 0, remove two elements ["bar", "baz"] and retains ["foo", "qux"].
10// //////////////////////////////////////
11var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
12for( var i = 0; i < arr.length; i++){
13 if ( arr[i] === 5) {
14 arr.splice(i, 1);
15 }
16}//=> [1, 2, 3, 4, 6, 7, 8, 9, 0]
17// //////////////////////////////////////
18var arr = [1, 2, 3, 4, 5, 5, 6, 7, 8, 5, 9, 0];
19for( var i = 0; i < arr.length; i++){
20 if ( arr[i] === 5) {
21 arr.splice(i, 1); i--;
22 }
23}//=> [1, 2, 3, 4, 6, 7, 8, 9, 0]
24// //////////////////////////////////////
25var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
26var filtered = array.filter(function(value, index, arr){
27 return value > 5;
28});//filtered => [6, 7, 8, 9]//array => [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
29// //////////////////////////////////////
30var evens = _.remove(array, function(n) {
31 return n % 2 === 0;
32});
33console.log(array);// => [1, 3]console.log(evens);// => [2, 4]
34// //////////////////////////////////////
35function arrayRemove(arr, value) {
36 return arr.filter(function(ele){
37 return ele != value; });
38}
39var result = arrayRemove(array, 6);// result = [1, 2, 3, 4, 5, 7, 8, 9, 0]
40// //////////////////////////////////////
41var ar = [1, 2, 3, 4, 5, 6];delete ar[4]; // delete element with index 4console.log( ar ); // [1, 2, 3, 4, undefined, 6]alert( ar ); // 1,2,3,4,,6
42