Remove Array Items Recursively in Lodash



In this problem, we have to remove certain items from arrays; this removal of array items needs to be done in a recursive manner for nested arrays. Lodash is a popular JavaScript library that helps us achieve this easily.

What is lodash?

Lodash is a popular JavaScript library used to deal with arrays, objects, and other data structures with ease. It has many helper functions such as map, filter, and invoke as well as function binding, JavaScript templating, deep equality checks, creating indexes, and much more.

Problem Description

We are given an array, and we have to remove an item from that array. In this article, we are going to discuss how we can remove array items recursively using Lodash.

Example 1

Input: 
array = [11, [12, 13, [14, 15]], 16]
number to be removed: 15 
Output:
[11, [12, 13, [14]], 16]

Explanation: After removing the number 15 from the nested array, the output array would be like this, having no 15 in the array.

Example 2

Input: 
array = [1, [2, 3, [4, 5]], 6]
number to be removed: 4
Output:
[1, [2, 3, [5]], 6]

Explanation: After removing the number 4 from the nested array, the output array would look like this, with no 4 in the array.

Below are different approaches to Remove Array Items Recursively in Lodash:

Using _.filter() Method

In this approach, we use _.filter inbuilt function, which is used to filter(remove) elements from an array based on some specific condition. This method doesn't modify the array in place, it returns a new array with the items that meet the condition.

Implementation Code

const _ = require('lodash');

const nestedArray = [
    1,
    2,
    [3, 4, 5],
    [6, 7, [8, 9, 10]],
    11
];

// Function to remove items recursively
function removeItemsRecursively(arr, condition) {
    _.forEach(arr, (item, index) => {
        if (Array.isArray(item)) {
            removeItemsRecursively(item, condition); // Recursive call for nested arrays
        } else {
            if (condition(item)) {
                _.remove(arr, (val, idx) => idx === index);
            }
        }
    });
}

// Remove all items equal to 5
removeItemsRecursively(nestedArray, (value) => value === 5);

console.log(nestedArray);

Output

[ 1, 2, [ 3, 4 ], [ 6, 7, [ 8, 9, 10 ] ], 11 ]

Using _.remove() Method

The _.remove () method of lodash is a powerful utility for removing specific items from an array. This method allows us to modify the array in place, removing all elements that match the given condition.

Implementation Code

const _ = require('lodash');

const nestedArray = [
    1,
    2,
    [3, 4, 5],
    [6, 7, [8, 9, 10]],
    11
];

// Function to remove items recursively
function removeItemsRecursively(arr, condition) {
    _.forEach(arr, (item, index) => {
        if (Array.isArray(item)) {
            removeItemsRecursively(item, condition); // Recursive call for nested arrays
        } else {
            if (condition(item)) {
                _.remove(arr, (val, idx) => idx === index);
            }
        }
    });
}

// Remove all items equal to 5
removeItemsRecursively(nestedArray, (value) => value === 5);

console.log(nestedArray);

Output

[ 1, 2, [ 3, 4 ], [ 6, 7, [ 8, 9, 10 ] ], 11 ]

Using _.flatMapDeep() and _.remove()

In this method, we first use _.flatMapDeep() to flatten the array and then use _.remove() to eliminate specific items. This approach is effective when you want to simplify the removal process by flattening the array first and then applying the removal logic.

Implementation Code

const _ = require('lodash');

const nestedArray = [
    1,
    2,
    [3, 4, 5],
    [6, 7, [8, 9, 10]],
    11
];

// Flatten the array deeply and then remove items
const flatArray = _.flatMapDeep(nestedArray, (item) => item);

_.remove(flatArray, (item) => item === 5);

console.log(flatArray);

Output

[ 1, 2, 3, 4, 6, 7, 8, 9, 10, 11 ]
Updated on: 2025-04-28T20:25:06+05:30

64 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements