lodash

Find object by match property in nested array

亡梦爱人 提交于 2019-11-28 06:51:43
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 } ] }, { name: 'Module2', submodules: [{ name: 'Submodule1', id: 3 }, { name: 'Submodule2', id: 4 } ] } ]; This won't work because submodules is an array, not an object. Is there any shorthand that would make this work? I'm trying to avoid iterating the array manually. _.where(modules, {submodules:{id:3}}); Here's what I came up with: _.find(modules, _.flow( _.property('submodules'), _.partialRight(_.some,

Filtering array of objects with lodash based on property value

痴心易碎 提交于 2019-11-28 06:42:17
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 is john with lodash? Dustin Poissant Lodash has a "map" function that works just like jQuerys: var myArr = [{ name: "john", age:23 }, { name: "john", age:43 }, { name: "jimi", age:10 }, { name: "bobi", age:67 }]; var johns = _.map(myArr, function(o) { if (o.name == "john") return o; }); // Remove undefines from the array johns = _.without(johns, undefined) Use lodash _.filter method: _.filter

Find all objects with matching Ids javascript

隐身守侯 提交于 2019-11-28 06:23:18
问题 I'm trying to get all objects with matching id's from my students array and get other property values from them... For instance my array looks like this: const students = [ {id: 1, name: 'Cal', location: 'McHale' }, {id: 2, name: 'Courtney', location: 'Sydney Hall' }, {id: 1, name: 'Cal', location: 'Syndey hall' } ] So my expected output would grab all instances of id: 1. {id: 1, name: 'Cal', location: 'McHale' }, {id: 1, name: 'Cal', location: 'Syndey hall' } I'll eventually want to remove

Intersection of characters in two strings

℡╲_俬逩灬. 提交于 2019-11-28 06:16:41
问题 I have an object with strings in it. filteredStrings = {search:'1234', select:'1245'} I want to return '124' I know that I can turn it into an array and then loop through each value and test if that value in inside of the other string, but I'm looking for an easier way to do this. Preferably with Lodash. I've found _.intersection(Array,Array) but this only works with Arrays. https://lodash.com/docs#intersection I want to be able to do this without having to convert the object to an array and

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

丶灬走出姿态 提交于 2019-11-28 05:21:12
This question already has an answer here: How to merge multiple array of object by ID in javascript? 4 answers 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" : ObjectId("575b0523a6f66a5732749ecb") }, { "member" : ObjectId("57989cbe54cf5d2ce83ff9d8"), "bank" : ObjectId("575b052ca6f66a5732749ecc"), "country" : ObjectId("575b0523a6f66a5732749ecb") }]; var arr2 = [{ "member" : ObjectId("57989cbe54cf5d2ce83ff9d6"), "name" : 'xxxxxx',

Map over object preserving keys

落花浮王杯 提交于 2019-11-28 03:30:43
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 preserve the keys? ie, I want a function that returns {one: 3, two: 6, three: 9} With Underscore Underscore provides a function _.mapObject to map the values and preserve the keys. _.mapObject({ one: 1, two: 2, three: 3 }, function (v) { return v * 3; }); // => { one: 3, two: 6, three: 9 } DEMO With Lodash Lodash provides a function _.mapValues to map

Angular2 - Angular-CLI installing lodash - Cannot find module

回眸只為那壹抹淺笑 提交于 2019-11-28 03:16:02
问题 Mac OSX El capitan | angular-cli: 0.1.0 | node: 5.4.0 | os: darwin x64 I try to install a 3rd party npm module according to the angular-cli wiki: https://github.com/angular/angular-cli/wiki/3rd-party-libs but fail. I've been struggling with this for days now and would greatly appreciate any help. Steps to get the error: ng new lodashtest3 cd lodashtest3 npm install lodash --save typings install lodash --ambient --save angular-cli-build.json: module.exports = function(defaults) { return new

Javascript: Sort array of arrays by second element in each inner array

末鹿安然 提交于 2019-11-28 02:02:45
I have an array that looks like this: const arr = [ [500, 'Foo'], [600, 'bar'], [700, 'Baz'], ]; I would like to sort this arr alphabetically by the second element in each inner array, ie: [ [600, 'bar'], [700, 'Baz'], [500, 'Foo'], ] Note the case insensitivity. Also, I would love to use lodash helpers if they come in handy here! Here is a concrete, working example, using Array.prototype.sort : const arr = [ [500, 'Foo'], [600, 'bar'], [700, 'Baz'] ]; arr.sort((a,b) => a[1].toUpperCase().localeCompare(b[1].toUpperCase())); console.log(arr); Array.prototype.sort takes a function which will be

Underscore.js findWhere nested objects

喜夏-厌秋 提交于 2019-11-27 23:49:21
I have an object of folders/files that looks like this: { about.html : { path : './about.html' }, about2.html : { path : './about2.html' }, about3.html : { path : './about3.html' }, folderName : { path : './folderName', children : { sub-child.html : { path : 'folderName/sub-child.html' } } } } And it can go 6-7 levels deep of folders having children. I want to find the object where path is equal to a string that I provide. Regardless of how deep it is. I'm using underscore which only does top level: _.findWhere(files,{path:'./about2.html'} How can I do a deep, nested search. Does underscore

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

放肆的年华 提交于 2019-11-27 22:10:07
问题 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 using lodash. It is easy to do this with a simple for loop, but it doesn't feel very elegant. for (var i = 0; i < arr.length - 1; i++) { var currentElement = arr[i]; var nextElement = arr[i + 1]; } Lodash almost can do this: _.forEach(_.zip(arr, _