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
43
votes
9 answers

Update if exists or add new element to array of objects - elegant way in javascript + lodash

So I have an array of objects like that: var arr = [ {uid: 1, name: "bla", description: "cucu"}, {uid: 2, name: "smth else", description: "cucarecu"}, ] uid is unique id of the object in this array. I'm searching for the elegant way to modify…
ganqqwerty
  • 1,894
  • 2
  • 23
  • 36
42
votes
8 answers

lodash: check object is empty

I have this object: {"": undefined} and when I check this object for empty in this way: _.isEmpty({"": undefined}) I get false result, maybe in lodash we have another method?
Meldum
  • 579
  • 1
  • 4
  • 6
42
votes
3 answers

lodash orderBy on nested property

I'm using v4.11.0. I would like sort objects based on milliseconds property. Here's the array : [ { "name": "bug12755.xml", "list": "bugs42", "start-date": "2015-09-14", "age": { "text": "7 months", …
Sudhakar
  • 2,904
  • 8
  • 33
  • 47
42
votes
1 answer

Where is _.pluck() in lodash version 4?

What happened to pluck() in lodash version 4? What is a suitable replacement? This syntax _.pluck(users, 'firstName'); is simple to me. Seems that _.map(users, function(user) { return user.firstName; } would do the trick but it's not nearly as neat.
Joe Hawkins
  • 9,803
  • 2
  • 21
  • 28
42
votes
2 answers

Lo-Dash, difference between array and collection

A glance at the Lo-Dash docs shows that the API falls in to categories of: Arrays, Chaining, Collections, Functions, Objects, Utilities, Methods, and Properties A more detailed look in to the Arrays API shows approximately 30…
aug2uag
  • 3,379
  • 3
  • 32
  • 53
41
votes
4 answers

How make lodash _.replace all occurrence in a string?

How to replace each occurrence of a string pattern in a string by another string? var text = "azertyazerty"; _.replace(text,"az","qu") return quertyazerty
Anthony
  • 3,989
  • 2
  • 30
  • 52
41
votes
13 answers

lodash: Get duplicate values from an array

Say I have an array like this: [1, 1, 2, 2, 3] I want to get the duplicates which are in this case: [1, 2] Does lodash support this? I want to do it in the shortest way possible.
zianwar
  • 3,642
  • 3
  • 28
  • 37
41
votes
2 answers

Sort array of JavaScript objects by property value

I have an array of JavaScript objects. My array is defined like this: var myObjects = [ { id: '1', username: 'bill.jones', active: true, createdon: '03/29/2014' }, { id: '2', username: 'woohoo', active: true, createdon: '03/28/2014' }, { id:…
user70192
  • 13,786
  • 51
  • 160
  • 240
39
votes
5 answers

React Native: Using lodash debounce

I'm playing around with React Native and lodash's debounce. Using the following code only make it work like a delay and not a debounce. { _.debounce(()=> console.log("debouncing"), 2000)() } /> I want the console…
Norfeldt
  • 8,272
  • 23
  • 96
  • 152
39
votes
6 answers

Map an array of arrays

Is there a method in lodash to map over an array of arrays I would like to do something like this so that it keeps the structure of the array. def double(x) { return x*2 } _([[1,2],[3,4]]).somemethod(double) == [[2,4],[6,8]]
bwbrowning
  • 6,200
  • 7
  • 31
  • 36
39
votes
6 answers

Find object by match property in nested array

I'm not seeing a way to find objects when my condition would involve a nested array. var modules = [{ name: 'Module1', submodules: [{ name: 'Submodule1', id: 1 }, { name: 'Submodule2', id: 2 } …
helion3
  • 34,737
  • 15
  • 57
  • 100
38
votes
1 answer

"Continue" in Lodash forEach

I was looking over the differences between the Underscore and Lodash libraries and I came upon one issue regarding _.each / _.forEach. In Underscore, the _.each function cannot break out of the looping. When using return false, it only worked as a…
IceWhisper
  • 807
  • 1
  • 11
  • 20
38
votes
8 answers

lodash orderby with null and real values not ordering correctly

I have an Angular 2 typescript application that is using lodash for various things. I have an array of objects that I am ordering using a property in the object... _.orderBy(this.myArray, ['propertyName'], ['desc']); This works well however my…
Ben Cameron
  • 4,335
  • 6
  • 51
  • 77
38
votes
9 answers

Set all Object keys to false

Lets say I have an object filter: { "ID": false, "Name": true, "Role": false, "Sector": true, "Code": false } I want to set all keys to false (to reset them). What's the best way to do this, I'd like to avoid looping with…
chefcurry7
  • 4,813
  • 11
  • 28
  • 33
38
votes
15 answers

Iterate an array as a pair (current, next) in JavaScript

In the question Iterate a list as pair (current, next) in Python, the OP is interested in iterating a Python list as a series of current, next pairs. I have the same problem, but I'd like to do it in JavaScript in the cleanest way possible, perhaps…
therealrootuser
  • 10,215
  • 7
  • 31
  • 46