Javascript Removing items from an array
Shift
Use .shift to remove the first item of an array.
var array = [1, 2, 3, 4];
array.shift();
Output
Pop
Further .pop is used to remove the last item from an array.
For example:
var array = [1, 2, 3];
array.pop();
Output
Splice
Use .splice() to remove a series of elements from an array. .splice() accepts two parameters, the starting index, and an optional number of elements to delete. If the second parameter is left out .splice() will remove all elements from the starting index through the end of the array.
var array = [1, 2, 3, 4];
array.splice(1, 2);
Output
The return of array.splice() is a new array containing the removed elements. For the example above, the return would be:
Output
Delete
Use delete to remove item from array without changing the length of array:
var array = [1, 2, 3, 4, 5];
console.log(array.length); // 5
delete array[2];
console.log(array); // [1, 2, undefined, 4, 5]
console.log(array.length); // 5
Array.prototype.length
Assigning value to length of array changes the length to given value. If new value is less than array length items will be removed from the end of value.
array = [1, 2, 3, 4, 5];
array.length = 2;
console.log(array);