lodash

Difference between “import { pick } from 'lodash';” and “import pick from 'lodash/pick';”

南楼画角 提交于 2019-12-01 22:37:26
问题 What's the difference between import { pick } from 'lodash'; and import pick from 'lodash/pick'; (Note that it's 'lodash/pick' in the second one, not just 'lodash' .) How does do they each affect the bundle size? Do they import exactly the same parts of lodash ? Are they comparatively fast? 回答1: The lodash module is a roll-up module that imports and reexports from its various individual modules like lodash/pick . So: import { pick } from 'lodash'; loads the full lodash module and then only

Difference between “import { pick } from 'lodash';” and “import pick from 'lodash/pick';”

我们两清 提交于 2019-12-01 21:06:02
What's the difference between import { pick } from 'lodash'; and import pick from 'lodash/pick'; (Note that it's 'lodash/pick' in the second one, not just 'lodash' .) How does do they each affect the bundle size? Do they import exactly the same parts of lodash ? Are they comparatively fast? The lodash module is a roll-up module that imports and reexports from its various individual modules like lodash/pick . So: import { pick } from 'lodash'; loads the full lodash module and then only imports the one function from it. import pick from 'lodash/pick'; loads only the lodash/pick module and gets

Join sequence of arrays with array delimeters (“intersperse”)

有些话、适合烂在心里 提交于 2019-12-01 19:39:57
Is there a function that lets me concat several arrays, with delimiters between them (the delimiters are also arrays), similarly to how join works but not restricted to strings? The function can be standard JS or part of a major library such as lodash (which is why it's referenced in the tags). Here is an example of usage: let numbers = [[1], [2], [3]]; let result = _.joinArrays(numbers, [0]); console.log(result); //printed: [1, 0, 2, 0, 3] This is analogous to: let strings = ["a", "b", "c"]; let result = strings.join(","); console.log(result); //printed: "a,b,c"; However, join can't be used

How to remove empty values from object using lodash

為{幸葍}努か 提交于 2019-12-01 18:52:23
问题 I have an object with several properties and I would like to remove objects/nested objects that are empty, using lodash. What is the best way to do this? Let template = { node: "test", representation: { range: { } }, transmit: { timeMs: 0 } }; to template = { node: "test", transmit: { timeMs: 0 } }; I tried something like this, but I am lost. Utils.removeEmptyObjects = function(obj) { return _.transform(obj, function(o, v, k) { if (typeof v === 'object') { o[k] = _.removeEmptyObjects(v); }

How to remove empty values from object using lodash

一个人想着一个人 提交于 2019-12-01 18:02:49
I have an object with several properties and I would like to remove objects/nested objects that are empty, using lodash. What is the best way to do this? Let template = { node: "test", representation: { range: { } }, transmit: { timeMs: 0 } }; to template = { node: "test", transmit: { timeMs: 0 } }; I tried something like this, but I am lost. Utils.removeEmptyObjects = function(obj) { return _.transform(obj, function(o, v, k) { if (typeof v === 'object') { o[k] = _.removeEmptyObjects(v); } else if (!_.isEmpty(v)) { o[k] = v; } }); }; _.mixin({ 'removeEmptyObjects': Utils.removeEmptyObjects });

Lodash - Find deep in array of object

白昼怎懂夜的黑 提交于 2019-12-01 15:55:25
I have an array of an object like this [ { 'a': 10, elements: [ { 'prop': 'foo', 'val': 10 }, { 'prop': 'bar', 'val': 25 }, { 'prop': 'test', 'val': 51 } ] }, { 'b': 50, elements: [ { 'prop': 'foo', 'val': 30 }, { 'prop': 'bar', 'val': 15 }, { 'prop': 'test', 'val': 60 } ] }, ] What I need is sum the property Val when prop is foo . So, I have to search through elements and get all objects where prop is foo . With this objects, I should sum the val property. I tried to use many combinations of _.find , _.pick and so on, but I don't get the right result. Can someone help me? Here's a solution

Lodash check value in a array case insensitive

a 夏天 提交于 2019-12-01 15:50:29
I am checking for a value in a array using lodash _.some function. but its case sensitive. Is there any function for case insensitive search in lodash. Below is my sample array structure [ { "Name": "Division 1", "ParentName": null }, { "Name": "Division 2", "ParentName": null } ] using lodash I am checking like this _.some(divisionList, ['Name', divisionname]); The "native" javascript (ES6) solution using Array.some() function (as an alternative): var divisionName = "division 2", // for example hasDivision = divisionList.some((obj) => obj["Name"].toLowerCase() === divisionName); console.log

Creating a .net like dictionary object in Javascript

心已入冬 提交于 2019-12-01 15:45:58
I want to create a object in JavaScript which will store values in key, value pair and I should be able to pass some key and should be able to get its value back. In .NET world we can use dictionary class for this kind of implementation. Do we have any option in JavaScript world? I am using ExtJs 4.1, so if you know of any option in ExtJS even that would work. I have tried something like this but I cannot get value by key. var Widget = function(k, v) { this.key = k; this.value = v; }; var widgets = [ new Widget(35, 312), new Widget(52, 32) ]; Just use a standard javascript object: var

Deep clone in TypeScript (preserving types)

女生的网名这么多〃 提交于 2019-12-01 14:50:51
问题 I need to deep clone an object in TypeScript. This shouldn't be a problem as libraries like Lodash provide appropriate functions for that. However, these seem to discard type information. > var a = new SomeClass(); > a instanceof SomeClass; < true > var b = _.cloneDeep(a); > b instanceof SomeClass; < false Is there a way to clone objects in TypeScript while preserving this typing information? 回答1: Typescript isn't discarding type information here. In the DefinitelyTyped lodash.d.ts file, you

Lodash check value in a array case insensitive

旧巷老猫 提交于 2019-12-01 14:45:28
问题 I am checking for a value in a array using lodash _.some function. but its case sensitive. Is there any function for case insensitive search in lodash. Below is my sample array structure [ { "Name": "Division 1", "ParentName": null }, { "Name": "Division 2", "ParentName": null } ] using lodash I am checking like this _.some(divisionList, ['Name', divisionname]); 回答1: The "native" javascript (ES6) solution using Array.some() function (as an alternative): var divisionName = "division 2", // for