lodash

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

旧城冷巷雨未停 提交于 2019-11-27 05:13:58
问题 Consider this example. I am using Lodash 'data': [ { 'category': { 'uri': '/categories/0b092e7c-4d2c-4eba-8c4e-80937c9e483d', 'parent': 'Food', 'name': 'Costco' }, 'amount': '15.0', 'debit': true }, { 'category': { 'uri': '/categories/d6c10cd2-e285-4829-ad8d-c1dc1fdeea2e', 'parent': 'Food', 'name': 'India Bazaar' }, 'amount': '10.0', 'debit': true }, { 'category': { 'uri': '/categories/d6c10cd2-e285-4829-ad8d-c1dc1fdeea2e', 'parent': 'Food', 'name': 'Sprouts' }, 'amount': '11.1', 'debit':

How to convert object containing Objects into array of objects

一曲冷凌霜 提交于 2019-11-27 04:12:23
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"} ] Thierry This works for me var newArrayDataOfOjbect = Object.values(data) In additional if you have key - value object try: const objOfObjs = { "one": {"id": 3}, "two": {"id": 4}, }; const arrayOfObj = Object.entries(objOfObjs).map((e) => ( { [e[0]]: e[1] } )); will return: [ "one": {"id": 3}, "two": {"id": 4}, ] var data = { a:{"0": "1"}, b:{"1": "2"}, c:{"2": "3"}, d:{"3": "4"} }; var myData = Object.keys(data).map

How to Import a Single Lodash Function?

跟風遠走 提交于 2019-11-27 04:12:01
问题 Using webpack, I'm trying to import isEqual since lodash seems to be importing everything. I've tried doing the following with no success: import { isEqual } from 'lodash' import isEqual from 'lodash/lang' import isEqual from 'lodash/lang/isEqual' import { isEqual } from 'lodash/lang' import { isEqual } from 'lodash/lang' 回答1: You can install lodash.isequal as a single module without installing the whole lodash package like so: npm install --save lodash.isequal When using ECMAScript 5 and

Create chain in lodash with custom functions

耗尽温柔 提交于 2019-11-27 03:23:11
问题 Is there way to get my own custom function in a chain of lodash. So for example like this: var l = [1,2,3] var add = function(a, b){return a+b} var r =_.chain(l).find(function(a){return a>1}).add(5).value() =>r = 7 回答1: What you look for is a way to extend the lodash prototype. It so nicely turns out that you can do it easily with a mixin utility function. Check here the docs: http://lodash.com/docs#mixin In your example it will look like: var l = [1,2,3]; var add = function(a, b){return a+b}

lodash property search in array and in nested child arrays

主宰稳场 提交于 2019-11-27 03:19:39
问题 I have this array: [ { id: 1, name: 'test 1', children: [] }, { id: 2, name: 'test 2', children: [ { id: 4, name: 'test 4' } ] }, { id: 3, name: 'test 3', children: [] } ] How can I filter by the id property in both this array and the nested children arrays? For example, searching for id = 3 , should return the test 3 object, and searching for id = 4 should return the test 4 object. 回答1: Using lodash, you can do something like this: _(data) .thru(function(coll) { return _.union(coll, _.pluck

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

橙三吉。 提交于 2019-11-27 03:14:29
My objects: [ { description: 'object1', id: 1 }, { description: 'object2', id: 2 } { description: 'object3', id: 3 } { description: 'object4', id: 4 } ] In my function below I'm passing in the description to find the matching ID: function pluckSavedView(action, view) { console.log('action: ', action); console.log('pluckSavedView: ', view); // view = 'object1' var savedViews = retrieveSavedViews(); console.log('savedViews: ', savedViews); if (action === 'delete') { var delete_id = _.result(_.find(savedViews, function(description) { return description === view; }), 'id'); console.log('delete_id:

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

為{幸葍}努か 提交于 2019-11-27 02:44:17
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" }, { "name": "Sam", "color": "blue", "age": "33" }, { "name": "eddie", "color": "green", "age": "77" } ] I want the _.groupBy function to return an object that looks like this: [ { color: "blue", users: [ { "name": "jim", "color": "blue", "age": "22" }, { "name": "Sam", "color": "blue", "age": "33" } ] }, { color: "green", users: [ { "name": "eddie", "color": "green", "age": "77" } ] } ]

Filtering array of objects with lodash based on property value

喜你入骨 提交于 2019-11-27 01:32:44
问题 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? 回答1: 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

Find property by name in a deep object

自古美人都是妖i 提交于 2019-11-27 01:16:17
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 can forget about infinite pointer recursion, this is a pure JSON response. fn({ 'a': 1, 'b': 2, 'c': {'d':{'e':7}}}, "d"); // [o.c] fn({ 'a': 1, 'b': 2, 'c': {'d':{'e':7}}}, "e"); // [o.c.d] fn({ 'aa': 1, 'bb': 2, 'cc': {'d':{'x':9}}, dd:{'d':{'y':9}}}, 'd'); // [o.cc,o.cc.dd] fwiw lodash has a _.find function that will find nested objects that are

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

这一生的挚爱 提交于 2019-11-27 01:06:39
问题 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}, { userId:"p2", profile:2} ]; I want to merge them based on "userId" to produce: [ { userId: 'p1', item: 1, profile: 1 }, { userId: 'p2', item: 2, profile:2 }, { userId: 'p3', item: 4 } ] I have these so far: var u = _.uniq(_.union(a, b), false, _.property('userId'));