delete item by index javascript array code example

Example 1: js remove item from array by value

var arr = ['bill', 'is', 'not', 'lame'];

arr.splice(output_items.indexOf('not'), 1);

console.log(arr) //returns ['bill', 'is', 'lame']

Example 2: remove item at index in array javascript

// remove element at certain index without changing original
let arr = [0,1,2,3,4,5]
let newArr = [...arr]
newArr.splice(1,1)//remove 1 element from index 1
console.log(arr) // [0,1,2,3,4,5]
console.log(newArr)// [0,2,3,4,5]

Example 3: remove array js

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