Q:

javascript array remove element

var colors = ["red","blue","car","green"];
var carIndex = colors.indexOf("car");//get  "car" index
//remove car from the colors array
colors.splice(carIndex, 1); // colors = ["red","blue","green"]
97
var colors = ["red","blue","car","green"];
var carIndex = colors.indexOf("car");//get  "car" index
//remove car from the colors array
colors.splice(carIndex, 1); // colors = ["red","blue","green"]
14
/* there are 3 ways to do so
1) pop(), which removes the last element
2) shift(), which removes the first element
3) splice(), which removes any element with a specified index.
of these only splice() takes parameters. the first parameter it takes is the
index of the element to be removed, an integer. the second is the number of 
elements to be removed, again integer. the third and fourth etc. are optional,
which is to be used if you want to replace them. Example: */
let numbers = [1, 2, 3, 4, 5];
numbers.pop(); // removes 5
numbers.shift(); // removes 1
let threeIndex = numbers.indexOf(3); // used for long arrays
numbers.splice(threeIndex, 1); // without replacement
numbers.splice(threeIndex, 1, 7) // 3 will now be replaced by 7
3
var myArray = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

//removing element using splice method -- 
//arr.splice(index of the item to be removed, number of elements to be removed)
//Here lets remove Sunday -- index 0 and Monday -- index 1
  myArray.splice(0,2)

//using filter method
let itemToBeRemoved = ["Sunday", "Monday"]
var filteredArray = myArray.filter(item => !itemToBeRemoved.includes(item))
12
const array = [2, 5, 9];

console.log(array);

const index = array.indexOf(5);
if (index > -1) {
  array.splice(index, 1);
}

// array = [2, 9]
console.log(array); 
12
let numbers = [1,2,3,4]

// to remove last element
let last_num = numbers.pop();
// numbers will now be [1,2,3]

// to remove first element
let first_num = numbers.shift();
//numbers will now be [2,3]

// to remove at an index
let number_index = 1
let index_num = numbers.splice(number_index,1); //removes 1 element at index 1
//numbers will now be [2]

//these methods will modify the numbers array and return the removed element
4
var ar = [1, 2, 3, 4, 5, 6];ar.length = 4; // set length to remove elementsconsole.log( ar ); // [1, 2, 3, 4]
// //////////////////////////////////////
var ar = [1, 2, 3, 4, 5, 6];ar.pop(); // returns 6console.log( ar ); // [1, 2, 3, 4, 5]
// //////////////////////////////////////
var ar = ['zero', 'one', 'two', 'three'];ar.shift(); // returns "zero"console.log( ar ); // ["one", "two", "three"]
// //////////////////////////////////////
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];var removed = arr.splice(2,2);
// //////////////////////////////////////
["bar", "baz", "foo", "qux"]list.splice(0, 2) // Starting at index position 0, remove two elements ["bar", "baz"] and retains ["foo", "qux"].
// //////////////////////////////////////
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
for( var i = 0; i < arr.length; i++){
  if ( arr[i] === 5) {
    arr.splice(i, 1); 
  }
}//=> [1, 2, 3, 4, 6, 7, 8, 9, 0]
// //////////////////////////////////////
var arr = [1, 2, 3, 4, 5, 5, 6, 7, 8, 5, 9, 0];
for( var i = 0; i < arr.length; i++){
  if ( arr[i] === 5) {
    arr.splice(i, 1); i--; 
  }
}//=> [1, 2, 3, 4, 6, 7, 8, 9, 0]
// //////////////////////////////////////
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
var filtered = array.filter(function(value, index, arr){
  return value > 5;
});//filtered => [6, 7, 8, 9]//array => [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
// //////////////////////////////////////
var evens = _.remove(array, function(n) {
  return n % 2 === 0;
});
console.log(array);// => [1, 3]console.log(evens);// => [2, 4]
// //////////////////////////////////////
function arrayRemove(arr, value) {
  return arr.filter(function(ele){
    return ele != value; });
}
var result = arrayRemove(array, 6);// result = [1, 2, 3, 4, 5, 7, 8, 9, 0]
// //////////////////////////////////////
var 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
1
let numbers = [1,2,3,4]

// to remove last element
let lastElem = numbers.pop()

// to remove first element
let firstElem = numbers.shift()

// both these methods modify array while returning the removed element
0
let values = [1,2,3,4,5,7,8,9,10]

// only keep n values from an array
values.length = 5
0

New to Communities?

Join the community