15 ways to remove elements from an array
Using Javascript with array mutation

Search for a command to run...
Using Javascript with array mutation

No comments yet. Be the first to comment.
You probably know better

The first rule of war and business is to know your enemy

Easy and responsive

Reusability with the least effort

It works no matter if it is real, FIFA, or eFootball

Good day fellow reader, today I will show you fifteen different ways to remove elements from a javascript array. Feel free to suggest more if you please!
Removing elements from an array might sound like a trivial task in many languages. However, in Javascript, there are multiple ways to achieve this desired result, depending on knowing the index of the item, or just knowing its value. Some options are more performant than others, some are easier to read, be sure to check them all out and use what best suits your needs.
You can remove an element or elements group of an array using:
Note: Filter will be kind of an exception here because every option presented mutates the original array. And filter does not. Still, it is advisable to use it if you don't need the original reference to the array.
Let's get there folks! We'll see 15 ways to remove items from an array!
Remember, all the methods presented mutate the original array. If you are interested in keeping the removed elements, look for the options that set the "removed" variable
This will just remove the last element. Simple, powerful, universal.
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let removed = arr.pop();
console.log(arr);
// [object Array] (8)
// [1,2,3,4,5,6,7,8]
You can truncate an array setting its length. Be warned, if you set it to a greater value than the current one, last element will be a non-iterable empty slot.
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
arr.length = 3;
console.log(arr);
// [object Array] (3)
// [1,2,3]
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let howmany = 7;
let removed = [];
if (howmany <= arr.length) {
removed = arr.splice(arr.length - howmany, howmany);
} else {
removed = arr.splice(0, arr.length);
}
console.log(arr);
// [object Array] (2)
// [1,2]
Removing only one element
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let removed = arr.shift();
console.log(arr);
// [object Array] (8)
// [2,3,4,5,6,7,8,9]
Removing multiple elements
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let howmany = 3;
let removed = arr.splice(0, howmany);
console.log(arr);
// [object Array] (6)
// [4,5,6,7,8,9]
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let from = 1;
let to = 3;
let removed = arr.splice(from, to);
console.log(arr);
// [object Array] (6)
// [1,5,6,7,8,9]
This ones assumes 'from' <= 'to'
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let from = 2;
let to = 5;
for (let i = from; i < arr.length - (to - from); i++) {
arr[i] = arr[i + (to - from)];
}
arr.length = arr.length - (to - from);
console.log(arr);
// [object Array] (6)
// [1,2,6,7,8,9]
Note: This one will leave undefined or empty spaces in the corresponding positions, the array size will not be changed.
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
delete arr[0];
delete arr[4];
console.log(arr);
// [object Array] (9)
// [,2,3,4,,6,7,8,9]
You need to install lodash in order to use this
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let removed = _.pullAt(arr, [0, 1, 8]);
console.log(arr);
// [object Array] (6)
// [3,4,5,6,7,8]
let people = [{
id: 15,
first_name: 'John',
last_name: 'Doe'
}, {
id: 16,
first_name: 'Jane',
last_name: 'Doe'
}, {
id: 20,
first_name: 'Guy',
last_name: 'Incognito'
}];
let indexToRemove = people.findIndex(elem => elem.first_name === 'John');
if (indexToRemove !== -1) {
people.splice(indexToRemove, 1);
}
console.log(people);
// [object Array] (2)
// [{
// id: 16,
// first_name: 'Jane',
// last_name: 'Doe'
// }, {
// id: 20,
// first_name: 'Guy',
// last_name: 'Incognito'
// }]
let people = [{
id: 15,
first_name: 'John',
last_name: 'Doe'
}, {
id: 16,
first_name: 'Jane',
last_name: 'Doe'
}, {
id: 20,
first_name: 'Guy',
last_name: 'Incognito'
}];
for (let i = people.length - 1; i >= 0; i--) {
if (people[i].last_name === 'Doe') {
people.splice(i, 1);
}
}
console.log(people);
// [object Array] (1)
// [{
// id: 20,
// first_name: 'Guy',
// last_name: 'Incognito'
// }]
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let removed = _.remove(arr, (item) => item > 3);
console.log(arr);
// [object Array] (3)
// [1,2,3]
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let removed = _.pull(arr, 6, 7, 8);
console.log(arr);
// [object Array] (6)
// [1,2,3,4,5,9]
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
arr.length = 0;
console.log(arr);
// [object Array] (0)
// []
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const newArr = words.filter(word => word != 'elite');
console.log(newArr);
// [object Array] (5)
// ["spray", "limit", "exuberant", "destruction", "present"]
Phew! This article took more time than I estimated. Remember to use whatever fits your needs. Hope you find it useful!
See you around!