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
48
votes
1 answer

How to break _.forEach in lodash?

I can't break _.forEach loop, help me to do that. _.forEach(oIncludedFileMap, function (aIncludedFiles, sKey) { if(aIncludedFiles == null){ break; } });
Mangesh Darekar
  • 505
  • 1
  • 5
  • 5
48
votes
2 answers

How to merge two arrays of objects by ID using lodash?

I have two array with one common field member. how can I merge theme easily? For example: var arr1 = [{ "member" : ObjectId("57989cbe54cf5d2ce83ff9d6"), "bank" : ObjectId("575b052ca6f66a5732749ecc"), "country" :…
Shaishab Roy
  • 16,335
  • 7
  • 50
  • 68
47
votes
2 answers

Deep Merge using Lodash

I have two arrays of objects that contain addresses that have a label and an object for the actual address: var originalAddresses = [ { label: 'home', address: { city: 'London', zipCode: '12345' } }, { label: 'work', address: {…
benjiman
  • 3,888
  • 4
  • 29
  • 44
47
votes
3 answers

"Count where" in a collection

Using lodash, what would be a good way to count the number of objects in a collection conditionally? Say I wanted to count the number of objects where a < 4 in the following collection [{a : 1}, {a : 2}, {a : 3}, {a : 4}, {a : 5}, {a : 6}]
swelet
  • 8,192
  • 5
  • 33
  • 45
47
votes
6 answers

How to use Lodash to merge two collections based on a key?

I have two collections, and the objects have a common key "userId". As below: var _= require('lodash'); var a = [ { userId:"p1", item:1}, { userId:"p2", item:2}, { userId:"p3", item:4} ]; var b = [ { userId:"p1", profile:1}, {…
murvinlai
  • 48,919
  • 52
  • 129
  • 177
46
votes
4 answers

How to correctly use Vue JS watch with lodash debounce

I'm using lodash to call a debounce function on a component like so: ... import _ from 'lodash'; export default { store, data: () => { return { foo: "", } }, watch: { searchStr:…
Artur Grigio
  • 5,185
  • 8
  • 45
  • 65
46
votes
13 answers

Lodash debounce with React Input

I'm trying to add debouncing with lodash to a search function, called from an input onChange event. The code below generates a type error 'function is expected', which I understand because lodash is expecting a function. What is the right way to do…
Michael Kaufman
  • 683
  • 1
  • 9
  • 19
46
votes
12 answers

Flatten array with objects into 1 object

Given input: [{ a: 1 }, { b: 2 }, { c: 3 }] How to return: { a: 1, b: 2, c: 3 } For arrays it's not a problem with lodash but here we have array of objects.
Szymon Toda
  • 4,454
  • 11
  • 43
  • 62
46
votes
1 answer

Sort items in an array by more than one field with lodash

How can I sort an array of objects by more then one field using lodash. So for an array like this: [ {a: 'a', b: 2}, {a: 'a', b: 1}, {a: 'b', b: 5}, {a: 'a', b: 3}, ] I would expect this result [ {a: 'a', b: 1}, {a: 'a', b: 2}, {a:…
Andreas Köberle
  • 106,652
  • 57
  • 273
  • 297
46
votes
10 answers

Find property by name in a deep object

I have a HUGE collection and I am looking for a property by key someplace inside the collection. What is a reliable way to get a list of references or full paths to all objects containing that key/index? I use jQuery and lodash if it helps and you…
Shanimal
  • 11,517
  • 7
  • 63
  • 76
45
votes
5 answers

Lodash debounce not working in React

it would be best to first look at my code: import React, { Component } from 'react'; import _ from 'lodash'; import Services from 'Services'; // Webservice calls export default class componentName extends Component { constructor(props) { …
user818700
45
votes
3 answers

Lodash / javascript : Compare two collections and return the differences

I have two arrays of objects: Elements of my tables are not primitive value, but complex objects. array1 = [obj1,obj2,obj3,obj4] array2 = [obj5,obj5,obj6,obj7] I would like to compare two arrays and see if the elements of array2 are already present…
Nacim Idjakirene
  • 1,882
  • 8
  • 26
  • 45
45
votes
5 answers

Lodash sort collection based on external array

I have an array with keys like so: ['asdf12','39342aa','12399','129asg',...] and a collection which has these keys in each object like so: [{guid: '39342aa', name: 'John'},{guid: '129asg', name: 'Mary'}, ... ] Is there a fast way to sort the…
silintzir
  • 762
  • 1
  • 6
  • 13
44
votes
3 answers

how to find difference between two array using lodash/underscore in nodejs

I have two arrays of arrays and am trying to find the difference. var a = [[ 11, 24, 28, 38, 42, 44 ], [ 7, 19, 21, 22, 29, 38 ], [ 2, 21, 27, 30, 33, 40 ], [ 6, 11, 12, 21, 34, 48 ], [ 1, 10, 17, 31, 35, 40 ], [ 1, 18, 26, 33, 36, 45 ], …
Firdous Alam
  • 563
  • 3
  • 8
  • 16
43
votes
16 answers

How to generate random hex string in javascript

How to generate a random string containing only hex characters (0123456789abcdef) of a given length?
Alessandro Dionisi
  • 2,494
  • 4
  • 33
  • 37