lodash

DefinitelyTyped Lodash

♀尐吖头ヾ 提交于 2019-12-10 16:20:26
问题 I want to import single function from lodash while preserving typings. Such as this: import invokeMap from 'lodash/invokeMap'; But it seems that I cannot do so. Compiler says that there is no module lodash/invokeMap . It's correct — DefinitelyTyped declares only top-level module for lodash , so I can import only the whole package. But I want to import only one function. Is there a way to import only typings information about lodash without importing lodash itself? Below is my workaround plan.

Why isn't lodash's _.unique returning unique objects when I pluck a field that is an object?

跟風遠走 提交于 2019-12-10 15:52:14
问题 I'm using lodash's _.unique and it's not working as expected. I'm doing this: uniqueByFocusIndex = _.unique(clickables, false, "focusIndex"); And as you can see in the image (look at the right), it's returning two elements with the same values for their focusIndex es. I'd expect this to return one of the two, not both. Is it because _.unique only works on primitives and not objects? Click to expand: 回答1: It doesn't work because comparing objects is done by reference and returns false even if

Compare two object with deep comparision or with json.stringify?

拟墨画扇 提交于 2019-12-10 14:56:36
问题 I am having a complex JSON object which I want to compare like below : $scope.new = [ { "name": "Node-1", "isParent": true, "text" : [ { "str" : "This is my first Node-1 string", "parent":[] }, { "str" : "This is my second Node-1 string", "parent":[] }], "nodes": [ { "name": "Node-1-1", "isParent": false, "text" : [ { "str" : "This is my first Node-1-1 string", "parent":[] }, { "str" : "This is my second Node-1-1 string", "parent":[] }], "nodes": [ { "name": "Node-1-1-1", "isParent": false,

Replace in array using lodash

左心房为你撑大大i 提交于 2019-12-10 14:40:41
问题 Is there an easy way to replace all appearances of an primitive in an array with another one. So that ['a', 'b', 'a', 'c'] would become ['x', 'b', 'x', 'c'] when replacing a with x . I'm aware that this can be done with a map function, but I wonder if have overlooked a simpler way. 回答1: In the specific case of strings your example has, you can do it natively with: myArr.join(",").replace(/a/g,"x").split(","); Where "," is some string that doesn't appear in the array. That said, I don't see

jasmine.clock().tick() does not work with $timeout and debounce, but works fine with setTimeout

夙愿已清 提交于 2019-12-10 13:59:51
问题 Below I have 3 functions that do exactly the same thing. Each one uses a different way of calling setTimeout, delay1() uses setTimeout directly, delay2() uses angularjs $timeout and delay3() uses lodash debounce. They all work fine. The problems occurs when I test using Jasmine. setTimeout works fine with the jasmine.clock().tick() method, but $timeout and debounce don't I am interested in getting debounce working with Jasmine. I know I can use $timeout.flush() with angularjs but $timeout and

List all possible paths using lodash

泪湿孤枕 提交于 2019-12-10 12:58:58
问题 I would like to list all paths of object that lead to leafs Example: var obj = { a:"1", b:{ foo:"2", bar:3 }, c:[0,1] } Result: "a","b.foo","b.bar", "c[0]","c[1]" I would like to find simple and readable solution, best using lodash. 回答1: Here is a solution that uses lodash in as many ways as I can think of: function paths(obj, parentKey) { var result; if (_.isArray(obj)) { var idx = 0; result = _.flatMap(obj, function (obj) { return paths(obj, (parentKey || '') + '[' + idx++ + ']'); }); }

Removing elements in an array using Lodash

寵の児 提交于 2019-12-10 12:28:52
问题 I have this array: var fruits = ['Apple', 'Banana', 'Orange', 'Celery']; And I use Lodash's remove like so: _.remove(fruits, function (fruit) { return fruit === 'Apple' || 'Banana' || 'Orange'; }) The result is ['Apple', 'Banana', 'Orange', 'Celery'] , while I expected it to be ['Apple', 'Banana', 'Orange'] . Why is this so? 回答1: Because when fruit is "Celery" , you are testing: "Celery" === 'Apple' || 'Banana' || 'Orange' which evaluates to false || true || true which is true . You can't use

Grouping an object with two attributes

匆匆过客 提交于 2019-12-10 12:25:59
问题 i have an api in rest that return the following object : The object [ { "attributes": { "State" : "Las vegas", "Period": 1991, "People": 6000, "Child" : 3000 } }, { "attributes": { "State" : "Las vegas", "Period": 2000, "People": 5000, "Child" : 1000 } } ] I need a group at the like with this : My previous return { "People_2000": [ { "State" : "Las vegas", "People": 5000 } ], "People_1991": [ { "State" : "Las vegas", "People": 6000 } ], "Child_1991": [ { "State" : "Las vegas", "Child": 3000 }

Nested _.max() with lodash

淺唱寂寞╮ 提交于 2019-12-10 10:23:50
问题 I am trying to get max and min values of arrays values inside objects array which looks like this: [ { id: 1, enabled: false, layer: 'Mood', color: '#f16c63', points: [ {date: '2013-01-02', value: 20}, {date: '2013-02-02', value: 15}, {date: '2013-03-12', value: 24}, {date: '2013-03-23', value: 18}, {date: '2013-03-24', value: 22}, {date: '2013-04-09', value: 12}, {date: '2013-06-13', value: 16}, {date: '2013-06-14', value: 20}, ] }, { id: 2, enabled: true, layer: 'Performance', color: '

Flatten an array of objects containing key\values

孤街浪徒 提交于 2019-12-10 10:17:53
问题 Say I have the follow array of objects storing a single key\value pair: var someArray = [ {foo: 1}, {bar: 2} ]; I would like to flatten this array into a single object, where each key from the original objects become a property of the new single object like so: someObject = { foo: 1, bar: 2 } I will always have unique keys, and only ever one key\value pairing. I know if I loop over the array in a foreach, then I can only access the index and value of the item, e.g. 0: {foo: 1} I want to be