Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

lodash
javascript
functions
communitycreator

What is the _.remove() method in Lodash?

Shubham Singh Kshatriya

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The _.remove() method in Lodash removes all the elements from an array that returns a truthy value for the specified predicate.

This method will mutate the original array and return an array of all the removed elements.

Syntax

_.remove(array, predicate)
The _.remove() method syntax

Parameters

This method accepts the following parameters:

  • array: This is the array to be inspected.
  • predicate: This is the function that is checked against the array's values in every iteration.

Return value

This method returns an array of all the removed elements.

Example

Let’s look at an example of the _.remove() method:

Console
The implementation of the _.remove() method

Explanation

In the HTML tab:

  • Line 5: We import the lodash script.

In the JavaScript tab:

  • Line 2: We create an array, numbers, and populate it with a few values.
  • Line 5: We use the _.remove() method to remove all the elements that are less than 5.
  • Line 8: We print the original array, numbers, on the console.
  • Line 11: We print the array of the removed elements, result, on the console.

Output

In the output, we see the original array mutated by the _.remove() method and another array that contains all the removed elements.

RELATED TAGS

lodash
javascript
functions
communitycreator

CONTRIBUTOR

Shubham Singh Kshatriya

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring