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
138
votes
8 answers

lodash: mapping array to object

Is there a built-in lodash function to take this: var params = [ { name: 'foo', input: 'bar' }, { name: 'baz', input: 'zle' } ]; And output this: var output = { foo: 'bar', baz: 'zle' }; Right now I'm just using…
core
  • 32,451
  • 45
  • 138
  • 193
119
votes
3 answers

Lodash : how to do a case insensitive sorting on a collection using orderBy?

I checked this answer but to achieve the same result, that is to get case-insensitive sorting, I need to use orderBy instead of sortBy since it gives the ability to specify the sort order. The only way I found to achieve it was to create a cloned…
WhiteEleven
  • 1,193
  • 2
  • 7
  • 6
114
votes
12 answers

Removing object properties with Lodash

I have to remove unwanted object properties that do not match my model. How can I achieve it with Lodash? My model is: var model = { fname: null, lname: null } My controller output before sending to the server will be: var credentials = { …
Alaksandar Jesus Gene
  • 6,523
  • 12
  • 52
  • 83
105
votes
5 answers

Split JavaScript array in chunks using Lodash

I need to split a JavaScript array into n sized chunks. E.g.: Given this array ["a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10", "a11", "a12", "a13"] and a n equals to 4, the output should be this: [ ["a1", "a2", "a3", "a4"], ["a5",…
Cesar Canassa
  • 18,659
  • 11
  • 66
  • 69
104
votes
12 answers

How to convert object containing Objects into array of objects

This is my Object var data = { a:{"0": "1"}, b:{"1": "2"}, c:{"2": "3"}, d:{"3": "4"} }; This is the output that I expect data = [ {"0": "1"}, {"1": "2"}, {"2": "3"}, {"3": "4"} ]
Nick Div
  • 5,338
  • 12
  • 65
  • 127
101
votes
26 answers

Convert returned JSON Object Properties to (lower first) camelCase

I have JSON returned from an API like so: Contacts: [{ GivenName: "Matt", FamilyName: "Berry" }] To keep this consistent with my code style (camelCase - lower case first letter) I want to transform the array to produce the following: contacts: [{…
Jon Wells
  • 4,191
  • 9
  • 40
  • 69
95
votes
6 answers

lodash debounce not working in anonymous function

Hello I cannot seem to figure out why the debounce function works as expected when passed directly to a keyup event; but it does not work if I wrap it inside an anonymous function. I have fiddle of the problem: http://jsfiddle.net/6hg95/1/ EDIT:…
Kristian Barrett
  • 3,574
  • 2
  • 26
  • 40
82
votes
4 answers

Why is lodash.each faster than native forEach?

I was trying to find the fastest way of running a for loop with its own scope. The three methods I compared were: var a = "t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t".split(); // lodash .each…
Matt Zukowski
  • 4,469
  • 4
  • 37
  • 38
76
votes
4 answers

How do you chain functions using lodash?

I have an object that looks like var foundUser = { charData: [] } which then I load an object from a database using mysql and then I call _.assignIn(foundUser, rows[0]); But I get a few extra properties that I don't need (this isn't solvable…
Datsik
  • 14,453
  • 14
  • 80
  • 121
75
votes
12 answers

Using lodash in all of vue component template

Can I use lodash _ in all of my vue component? for example: I have components organized like below: App.vue > Parent.vue > Child.vue I would like all of my component to access _ lodash without defined in every component vm data === I am also trying…
antoniputra
  • 4,251
  • 6
  • 26
  • 31
75
votes
8 answers

Filtering array of objects with lodash based on property value

We have an array of objects as such var myArr = [ {name: "john", age: 23}, {name: "john", age: 43}, {name: "jim", age: 101}, {name: "bob", age: 67} ]; how do I get the list of objects from myArr where name…
sarsnake
  • 26,667
  • 58
  • 180
  • 286
75
votes
6 answers

Comparing two arrays of objects, and exclude the elements who match values into new array in JS

here is my use case in JavaScript: I have two arrays of objects which have properties that match (id & name). var result1 = [ {id:1, name:'Sandra', type:'user', username:'sandra'}, {id:2, name:'John', type:'admin', username:'johnny2'}, …
Leo
  • 5,363
  • 5
  • 27
  • 30
75
votes
5 answers

Lodash: how do I use filter when I have nested Object?

Consider this example. I am using Lodash 'data': [ { 'category': { 'uri': '/categories/0b092e7c-4d2c-4eba-8c4e-80937c9e483d', 'parent': 'Food', 'name': 'Costco' }, …
daydreamer
  • 87,243
  • 191
  • 450
  • 722
74
votes
9 answers

Using lodash push to an array only if value doesn't exist?

I'm trying to make an array that if a value doesn't exist then it is added but however if the value is there I would like to remove that value from the array as well. Feels like Lodash should be able to do something like this. I'm interested in…
Max Lynn
  • 1,738
  • 6
  • 22
  • 35
67
votes
12 answers

How to remove undefined values from array but keep 0 and null

In javascript, I want to remove undefined values, but keep the values 0 and null from an array. [ 1, 2, 3, undefined, 0, null ] How can I do it cleanly?
JLavoie
  • 16,678
  • 8
  • 33
  • 39