9 Ways to Remove Elements from Arrays in JavaScript

jsdevspace

JSDev Space

Posted on August 6, 2024

9 Ways to Remove Elements from Arrays in JavaScript

Removing elements from arrays in JavaScript can be done using various methods, depending on whether you want to modify the original array or create a new one without certain elements. Here are five common ways to remove elements from arrays in JavaScript:

1. Using splice method

The splice(start, deleteCount, item1ToAdd, item2ToAdd, ...) method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.

Example: Remove elements at specific index:

const colors = ['blue', 'red', 'yellow', 'green'];

// Remove one element starting at index 1
colors.splice(1, 1);

// Output: ['blue', 'yellow', 'green']
console.log(colors);
Enter fullscreen mode Exit fullscreen mode

Example: Remove elements and replace with new elements:

const colors = ['blue', 'red', 'yellow', 'green'];

// Remove 1 elements starting at index 1 and replace with 'purple' and 'orange'
colors.splice(1, 1, 'purple', 'orange');

// Output: ['blue', 'purple', 'orange', 'yellow', 'green']
console.log(colors);
Enter fullscreen mode Exit fullscreen mode

2. Using pop method

The pop() method removes the last element from an array and returns that element. This method changes the length of the array.

Example: Remove the last element:

const colors = ['blue', 'red', 'yellow', 'green'];

// Remove 1 elements starting at index 1 and replace with 'purple' and 'orange'
const removed = colors.pop();

// Output: 'green'
console.log(removed);
// Output: ['blue', 'red', 'yellow']
console.log(colors);
Enter fullscreen mode Exit fullscreen mode

3. Using shift method

The shift() method removes the first element from an array and returns that removed element. This method changes the length of the array.

Example: Remove the first element:

const colors = ['blue', 'red', 'yellow', 'green'];

// Remove 1 elements starting at index 1 and replace with 'purple' and 'orange'
const removed = colors.shift();

// Output: 'blue'
console.log(removed);
// Output: ['red', 'yellow', 'green']
console.log(colors);
Enter fullscreen mode Exit fullscreen mode

4. Using fileter method

The filter(callback(element[, index, array]), thisArg) method creates a new array with all elements that pass the test implemented by the provided function. It does not modify the original array; instead, it returns a new array containing only the elements that satisfy the condition.

Example: Remove elements based on condition:

const colors = ['blue', 'red', 'yellow', 'green'];

// Remove 1 elements starting at index 1 and replace with 'purple' and 'orange'
const removed = colors.filter((color) => color !== 'red');

// Output: ['blue', 'yellow', 'green'] NEW ARRAY!
console.log(removed);
// Output: ['blue', 'red', 'yellow', 'green'] Old array
console.log(colors);
Enter fullscreen mode Exit fullscreen mode

5. Using slice method

The slice(start, end) method returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included). This method does not modify the original array.

Example: Create a new array without the second element:

const colors = ['blue', 'red', 'yellow', 'green'];

// Create a new array without the element at index 1
const newColors = colors.slice(0, 1).concat(colors.slice(2));

// Output: ['blue', 'yellow', 'green']
console.log(newColors);
Enter fullscreen mode Exit fullscreen mode

6. Using map method

The map(function callback(currentValue, index, array)) function transforms each element of an array based on the provided callback function. If you want to remove elements, you can conditionally return undefined or an empty array ([]) for those elements you wish to exclude.

Example: Remove the red color(s) from an array

const colors = ['blue', 'red', 'yellow', 'green'];

// Remove the red color(s)
const newColors = colors
    .map((color) => {
        if (color === 'red') {
            return undefined;
        } else {
            return color;
        }
    })
    .filter((color) => color !== undefined);

// Output: ['blue', 'yellow', 'green']
console.log(newColors);
Enter fullscreen mode Exit fullscreen mode

In this example:

map is used to iterate over each element (num) in the numbers array.

If color is red (color === 'red'), it is returned to keep it in the resulting array.

If color is red, undefined is returned, effectively removing it from the array.

filter is then used to remove all undefined values from the resulting array.

7. Using flatMap method

The flatMap() method first maps each element using a mapping function, then flattens the result into a new array. Similar to map(), you can conditionally return empty arrays ([]) for elements you want to remove.

Example: Remove elements containing 'yellow'

const colors = ['blue', 'red', 'yellow', 'green'];

// Remove the yellow color(s)
const newColors = colors.flatMap((color) => {
    if (color === 'yellow') {
        return [];
    } else {
        return [color];
    }
});

// Output: ['blue', 'red', 'green']
console.log(newColors);
Enter fullscreen mode Exit fullscreen mode

8. Using delete operator

In JavaScript, the delete operator is used to remove a property from an object or an element from an array. However, its behavior differs slightly depending on what you are trying to delete.

Example: Remove element with delete operator:

const colors = ['blue', 'red', 'yellow', 'green'];

// Remove the second element
delete colors[1];

// Output: ['blue', 'yellow', 'green']
console.log(colors);
Enter fullscreen mode Exit fullscreen mode

9. Using spread operator

The spread operator (...) is a convenient way to copy or combine arrays and objects, but it doesn’t directly delete elements from an array. However, you can use the spread operator in combination with other methods to effectively remove elements from an array and create a new array without those elements.

You can use the spread operator along with slice() or filter() to create a new array without a specific element.

const colors = ['blue', 'red', 'yellow', 'green'];
const indexToRemove = 2;

// The spread operator ... combines these two slices into a new array.
const newColors = [
    //  Extracts elements before the index to remove.
    ...colors.slice(0, indexToRemove),
    // Extracts elements after the index to remove.
    ...colors.slice(indexToRemove + 1),
];

// Output: ['blue', 'red', 'green']
console.log(newColors);
Enter fullscreen mode Exit fullscreen mode

Using spred operator allows you to handle array element removal in a functional and immutable way, maintaining clean and readable code.

Each method has its own use case and benefits, depending on whether you need to modify the original array or create a new one, and whether you need to remove elements based on their value, index, or a condition.

Also published here

💖 💪 🙅 🚩
jsdevspace
JSDev Space

Posted on August 6, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related