lodash

lodash filter on key with multiple values

戏子无情 提交于 2019-12-05 00:15:06
问题 I am trying to find out in lodash javascript library , how to find out filter array of objects multiple values of key. something similar to SQL - WHERE KEY in (val1, val2) Having said, with following example : var users = [ { 'user': 'barney', 'age': 36, 'active': true }, { 'user': 'fred', 'age': 40, 'active': false }, { 'user': 'Avding', 'age': 34, 'active': true } ]; _.filter(...) How can I find users, who's age in (34, 36) ?? numbers can also change runtime 回答1: Lodash's filter accepts a

Backbone / Underscore chain method with where method

我只是一个虾纸丫 提交于 2019-12-04 23:33:22
There has to be something simple I am missing here. http://jsfiddle.net/v9mdZ/ I am just learning Backbone and Underscore/loDash and am trying to get familiar with chain . I have the following code, which works as expected: var ids = _.pluck(collection.where({'is_checked':true}), 'id'); I attempted to refactor this, using chain like so: var ids = collection.chain().where({'is_checked':true}).pluck('id').value(); Why doesn't the refactored code work? Am I using chain wrong? Solution (details below) Don't use where with chain . The merging of some Underscore methods into collections is a little

Lodash for Python?

岁酱吖の 提交于 2019-12-04 23:15:26
Is there a library or something similar to lodash, for Python? We use the library extensively on our API and while we move on to creating a series of Python workers, it would make sense to create a similar structure to our API syntactics. pydash is exactly like lodash, only for Python. pydash is something which you can use as a replacement of lodash in python. Almost everything is covered under it, the names of functions are also similar just keeping python's conventions as is. Important: I've been using pydash for around 07 months, It helped me a lot. One thing which you should not forget is,

Combine objects with the same key with lodash

断了今生、忘了曾经 提交于 2019-12-04 19:14:43
I have an array of objects. I need to combine all the objects on the array that have the same key. This is the original array: [ { foo: "A", bar: [ { baz: "1", qux: "a" }, { baz: "2", qux: "b" } ] }, { foo: "B", bar: [ { baz: "3", qux: "c" }, { baz: "4", qux: "d" } ] }, { foo: "A", bar: [ { baz: "5", qux: "e" }, { baz: "6", qux: "f" } ] }, { foo: "B", bar: [ { baz: "7", qux: "g" }, { baz: "8", qux: "h" } ] } ] I need to combine the objects so the output is as follows: [ { foo: "A", bar: [ { baz: "1", qux: "a" }, { baz: "2", qux: "b" }, { baz: "5", qux: "e" }, { baz: "6", qux: "f" } ] }, { foo:

lodash sortBy then groupBy, is order maintained?

大憨熊 提交于 2019-12-04 19:07:45
问题 I'm having trouble figuring out from the lodash documentation if my assumption about sorting and grouping is correct. If I use sortBy, then use groupBy, do the arrays produced by groupBy maintain the sort order of items? For example, say I have the following array: var testArray = [[5,6],[1,3],[5,4],[5,1]] And I would like to group these by their first element, but also have them sorted by their second element within these groups. So, in lodash I assume I can do the following: _.chain

Underscore.js - get unique property values

南楼画角 提交于 2019-12-04 17:08:15
问题 I only recently discovered the power of underscore.js, still new to the methods I kindly ask for a suggestion: How do I get from this: [ [{ "name": "Type 2", "id": 14 }], [{ "name": "Type 1", "id": 13 }, { "name": "Type 3", "id": 15 }], [{ "name": "Type 2", "id": 14 }], [{ "name": "Type 1", "id": 13 }] ] to this: ["Type 1","Type 2","Type 3"] i.e. no duplicated and "name" property only. Any suggestions much appreciated. 回答1: _(data).chain().flatten().pluck('name').unique().value() (Convert the

Filter array of objects that contains string using lodash

旧城冷巷雨未停 提交于 2019-12-04 16:55:20
问题 I have a value and i need to return the objects that contains this value in propertie. var search='CPP@'; var results=_.filter(collection,{VAL:search}); I need to grab all objects that constains 'CPP@' , not the equals. I've prepared a https://jsfiddle.net/licass/e87mxfqt/ 回答1: var collection=[ { "DSP_MAQ": "Máquina 4", "VAL": "CPP@4@1900-01-01" }, { "DSP_MAQ": "Máquina 5", "VAL": "CMIP@5@1900-01-01" }, { "DSP_MAQ": "Máquina 6", "VAL": "CMIP@6@1900-01-01" }, { "DSP_MAQ": "Máquina 7", "VAL":

Flattening deeply nested array of objects

时光总嘲笑我的痴心妄想 提交于 2019-12-04 15:25:41
I got following data structure, which is an array of accounts objects, where some accounts are being parents to its children accounts, which in turn can be parents to other accounts etc.: [{ "id": "acc.1260446672222.11", "type": "EXPENSES_FOLDER", "name": "Expense Group", "balance": 3418.11, "children": [{ "id": "acc.1260446672238.27", "type": "EXPENSE", "name": "Advertising, Promotion and Entertainment Account", "balance": 0, "children": [] }, { "id": "acc.9a2492ba-0d82-4f4a-a1b4-14868f1e1a39", "type": "EXPENSES_FOLDER", "name": "Premises Costs", "balance": 0, "children": [{ "id": "acc

TypeScript Type-safe Omit Function

旧巷老猫 提交于 2019-12-04 13:05:01
问题 I want to replicate lodash's _.omit function in plain typescript. omit should return an object with certain properties removed specified via parameters after the object parameter which comes first. Here is my best attempt: function omit<T extends object, K extends keyof T>(obj: T, ...keys: K[]): {[k in Exclude<keyof T, K>]: T[k]} { let ret: any = {}; let key: keyof T; for (key in obj) { if (!(keys.includes(key))) { ret[key] = obj[key]; } } return ret; } Which gives me this error: Argument of

Is there a performance difference between import * as _ from 'lodash' and import { indexOf } from 'lodash'

醉酒当歌 提交于 2019-12-04 11:16:55
I was wondering if there is any difference in memory and performance between the 2 import. If I have lodash in my node module, does it compile all the file anyway no matter the import? In theory, based on the specification for import , yes, there is supposed to be a difference. The specification allows for a compliant optimization to use static analysis of a named import in order to only load what is required to provide indexOf() , if the lodash module is written as an ES2015 module. It would create an ImportEntry record that keeps references to how to resolve the import when running static