lodash

Using LoDash with EmberCLI

◇◆丶佛笑我妖孽 提交于 2020-01-22 13:56:25
问题 Does anyone have a working example of a (simple) ember-app project built using Ember-CLI that uses LoDash? (e.g.: I want to use lodash, _.someLodashFunc, in my Routes and Controllers). I haven't seen any thread / article on the web that gives clear, step-by-step explanation on how to do that. If possible with lodash v3.0.0 (and I'm using the latest ember-cli, v0.1.9). Thanks, Raka I found out how, you need to generate a custom build of lodash (the "lodash modern"). Use lodash-cli: https:/

How to merge array of objects using lodash

亡梦爱人 提交于 2020-01-22 02:26:32
问题 I would like to merge all objects in one array into one object, with a custom function. Using lodash's mergeWith works well: let a = [{a: [1,2]}, {a:[3,4]}, {a: [7,8]}] let b = mergeWith( a[0], ...a.slice(1), (objValue: any, srcValue: any) => { if (Array.isArray(objValue)) { return objValue.concat(srcValue); } }, ); console.log(b); // result: {a:[1,2,3,4,7,8]} This works fine but it seems wasteful to create an array copy just for that ( a.slice(1) ) - is there another way to pass that array

How to sort an array of timestamps using lodash in desc order

末鹿安然 提交于 2020-01-21 08:11:47
问题 I want to sort the following array of timestamps using lodash so the latest time stamp is first [3]. Code: let timestamps = ["2017-01-15T19:18:13.000Z", "2016-11-24T17:33:56.000Z", "2017-04-24T00:41:18.000Z", "2017-03-06T01:45:29.000Z", "2017-03-05T03:30:40.000Z"] const sorted = _.sortBy(timestamps); This does not work as i expect, i believe its sorting them but in asc order. 回答1: How to sort an array of timestamps using lodash This code is already sorting timestamps correctly using lodash:

Update if exists or add new element to array of objects - elegant way in javascript + lodash

三世轮回 提交于 2020-01-19 12:17:27
问题 So I have an array of objects like that: var arr = [ {uid: 1, name: "bla", description: "cucu"}, {uid: 2, name: "smth else", description: "cucarecu"}, ] uid is unique id of the object in this array. I'm searching for the elegant way to modify the object if we have the object with the given uid, or add a new element, if the presented uid doesn't exist in the array. I imagine the function to be behave like that in js console: > addOrReplace(arr, {uid: 1, name: 'changed name', description:

Update if exists or add new element to array of objects - elegant way in javascript + lodash

谁说我不能喝 提交于 2020-01-19 12:17:24
问题 So I have an array of objects like that: var arr = [ {uid: 1, name: "bla", description: "cucu"}, {uid: 2, name: "smth else", description: "cucarecu"}, ] uid is unique id of the object in this array. I'm searching for the elegant way to modify the object if we have the object with the given uid, or add a new element, if the presented uid doesn't exist in the array. I imagine the function to be behave like that in js console: > addOrReplace(arr, {uid: 1, name: 'changed name', description:

Update if exists or add new element to array of objects - elegant way in javascript + lodash

人走茶凉 提交于 2020-01-19 12:16:43
问题 So I have an array of objects like that: var arr = [ {uid: 1, name: "bla", description: "cucu"}, {uid: 2, name: "smth else", description: "cucarecu"}, ] uid is unique id of the object in this array. I'm searching for the elegant way to modify the object if we have the object with the given uid, or add a new element, if the presented uid doesn't exist in the array. I imagine the function to be behave like that in js console: > addOrReplace(arr, {uid: 1, name: 'changed name', description:

Lodash: Find and add an item in a multi-level Json based on a conditions

怎甘沉沦 提交于 2020-01-17 17:16:52
问题 I am trying to add an item to "roles array" based on a condition. My json: [{ "unitId": "2", name: "elizabeth", roles: [{ "role": { "roleId": "2", roleName: "testing" } }, { "role": { "roleId": "5", roleName: "dev" } }] }, { "unitId": "3", name: "peter", roles: [{ "role": { "roleId": "1", roleName: "testing" } }, { "role": { "roleId": "2", roleName: "dev" } }] } ] let newRole = { "role":{"roleId" : "6", roleName: "BA"}} Expected result: After adding the newRole to the roles array. [{ "unitId"

Lodash: Find and add an item in a multi-level Json based on a conditions

孤者浪人 提交于 2020-01-17 17:15:49
问题 I am trying to add an item to "roles array" based on a condition. My json: [{ "unitId": "2", name: "elizabeth", roles: [{ "role": { "roleId": "2", roleName: "testing" } }, { "role": { "roleId": "5", roleName: "dev" } }] }, { "unitId": "3", name: "peter", roles: [{ "role": { "roleId": "1", roleName: "testing" } }, { "role": { "roleId": "2", roleName: "dev" } }] } ] let newRole = { "role":{"roleId" : "6", roleName: "BA"}} Expected result: After adding the newRole to the roles array. [{ "unitId"

Lodash: Find and add an item in a multi-level Json based on a conditions

柔情痞子 提交于 2020-01-17 17:15:37
问题 I am trying to add an item to "roles array" based on a condition. My json: [{ "unitId": "2", name: "elizabeth", roles: [{ "role": { "roleId": "2", roleName: "testing" } }, { "role": { "roleId": "5", roleName: "dev" } }] }, { "unitId": "3", name: "peter", roles: [{ "role": { "roleId": "1", roleName: "testing" } }, { "role": { "roleId": "2", roleName: "dev" } }] } ] let newRole = { "role":{"roleId" : "6", roleName: "BA"}} Expected result: After adding the newRole to the roles array. [{ "unitId"

group array with sub array

孤街浪徒 提交于 2020-01-17 04:36:05
问题 I'm trying to find the easiest way to take an array that has like information and create an array with sub array. The newly created parent array should also sum the values that are found in the sub-array. This is the equivalent of an SQL statement that uses sum/group by. This is the original array [ { 'userId':1, 'userName':'bob', 'category':'shoes', 'count':2 }, { 'userId':1, 'userName':'bob', 'category':'rocks', 'count':4 }, { 'userId':1, 'userName':'bob', 'category':'bags', 'count':3 }, {