Questions tagged [lodash]

A modern JavaScript utility library delivering modularity, performance, and extras.

Lodash is a JavaScript utility library delivering consistency, customization, performance, and extra features. It includes an Underscore.js build that can be used as a replacement for , and indeed its functionality is a superset of Underscore.js's.

It comes in a "modern" build for newer browsers, a compatibility build for older browsers and , and per-method builds (individual features).

Lodash’s modular methods are great for:

  • Iterating arrays, objects, & strings
  • Manipulating and testing values
  • Creating composite functions
  • Creating your own mixin for Lodash

Useful links:

6953 questions
201
votes
9 answers

Angular 4 HttpClient Query Parameters

I have been looking for a way to pass query parameters into an API call with the new HttpClientModule's HttpClient and have yet to find a solution. With the old Http module you would write something like this. getNamespaceLogs(logNamespace) { …
joshrathke
  • 7,564
  • 7
  • 23
  • 38
198
votes
10 answers

How can I remove an element from a list, with lodash?

I have an object that looks like this: var obj = { "objectiveDetailId": 285, "objectiveId": 29, "number": 1, "text": "x", "subTopics": [{ "subTopicId": 1, "number": 1 }, { "subTopicId": 2, …
Samantha J T Star
  • 30,952
  • 84
  • 245
  • 427
193
votes
7 answers

lodash multi-column sortBy

There's a nifty method to sort an array of objects based on several properties: var data = _.sortBy(array_of_objects, ['type', 'name']); However that is only for ascending sorting. Is there some handy way of defining direction per column? E.g. var…
nhaa123
  • 9,570
  • 11
  • 42
  • 63
190
votes
10 answers

How to use lodash to find and return an object from Array?

My objects: [ { description: 'object1', id: 1 }, { description: 'object2', id: 2 } { description: 'object3', id: 3 } { description: 'object4', id: 4 } ] In my function below I'm…
Leon Gaban
  • 36,509
  • 115
  • 332
  • 529
187
votes
3 answers

How do I use the includes method in lodash to check if an object is in the collection?

lodash lets me check for membership of basic data types with includes: _.includes([1, 2, 3], 2) > true But the following doesn't work: _.includes([{"a": 1}, {"b": 2}], {"b": 2}) > false This confuses me because the following methods that search…
Conrad.Dean
  • 4,341
  • 3
  • 32
  • 41
181
votes
9 answers

Using lodash to compare jagged arrays (items existence without order)

I know I can do it using loops, but I'm trying to find an elegant way of doing this: I have two jagged arrays (array of arrays): var array1 = [['a', 'b'], ['b', 'c']]; var array2 = [['b', 'c'], ['a', 'b']]; I want to use lodash to confirm that the…
pQuestions123
  • 4,471
  • 6
  • 28
  • 59
177
votes
16 answers

Lodash title case (uppercase first letter of every word)

I'm looking through the lodash docs and other Stack Overflow questions - while there are several native JavaScript ways of accomplishing this task, is there a way I can convert a string to title case using purely lodash functions (or at least…
brandonscript
  • 68,675
  • 32
  • 163
  • 220
177
votes
16 answers

is there a function in lodash to replace matched item

I wonder if there is a simpler method in lodash to replace an item in a JavaScript collection? (Possible duplicate but I did not understand the answer there:) I looked at their documentation but could not find anything My code is: var arr = [{id: 1,…
Vishal Seth
  • 4,948
  • 7
  • 26
  • 28
171
votes
12 answers

How to repeat an element n times using JSX and Lodash

I am using React/JSX and Lodash in my app in order to accomplish what I want. I need to repeat an element a certain amount of times depending on a condition. How should I do that? Here is the element: ; And I…
StillDead
  • 1,879
  • 2
  • 10
  • 12
159
votes
17 answers

Replacing objects in array

I have this javascript object: var arr1 = [{id:'124',name:'qqq'}, {id:'589',name:'www'}, {id:'45',name:'eee'}, {id:'567',name:'rrr'}] var arr2 = [{id:'124',name:'ttt'}, {id:'45',name:'yyy'}] I need to…
Michael
  • 13,950
  • 57
  • 145
  • 288
154
votes
9 answers

Loop through properties in JavaScript object with Lodash

Is it possible to loop through the properties in a JavaScript object? For instance, I have a JavaScript object defined as this: myObject.options = { property1: 'value 1', property2: 'value 2' }; Properties will get dynamically added to this…
user3111277
  • 2,237
  • 3
  • 17
  • 19
153
votes
12 answers

Map over object preserving keys

The map function in underscore.js, if called with a javascript object, returns an array of values mapped from the object's values. _.map({one: 1, two: 2, three: 3}, function(num, key){ return num * 3; }); => [3, 6, 9] is there a way to make it…
xuanji
  • 5,007
  • 2
  • 26
  • 35
152
votes
3 answers

use Lodash to sort array of object by value

I am trying to sort an array by 'name' value (using Lodash). I used the Lodash docs to create the solution below however .orderBy doesn't seem to be having any affect at all. Can anyone shed some light on the correct way to sort array? Chars Array […
user4831663
147
votes
11 answers

using lodash .groupBy. how to add your own keys for grouped output?

I have this sample data returned from an API. I'm using Lodash's _.groupBy to convert the data into an object I can use better. The raw data returned is this: [ { "name": "jim", "color": "blue", "age": "22" }, { …
user1767105
  • 1,919
  • 3
  • 15
  • 10
139
votes
28 answers

How to use throttle or debounce with React Hook?

I'm trying to use the throttle method from lodash in a functional component, e.g.: const App = () => { const [value, setValue] = useState(0) useEffect(throttle(() => console.log(value), 1000), [value]) return (
Alexandre Annic
  • 9,942
  • 5
  • 36
  • 50