lodash

Should host objects be counted as plain objects in an isPlainObject function?

半城伤御伤魂 提交于 2019-11-28 14:45:16
I've been doing some testing of isPlainObject functions from different libraries on different browsers. There are 4 different (code wise) isPlainObject functions being tested on a wide range of objects: jquery lodash utility (a library that I'm working on) alternative, suggested in comments below All four of the above show differences on Chrome v23.0.1271.95 through to Chrome v25.0.1364.160, FireFox v 19.0 an Opera v12.14, but utility at least gives the same response of false for these object on all the browsers The tests on jsfiddle when run on Chrome Failed to agree: JSON - jquery: true -

Find an object in an array of deeply nested objects recursively

杀马特。学长 韩版系。学妹 提交于 2019-11-28 12:55:38
I have an array, and each item is an object with unique Ids. Some items may also have children, and it can have children array inside a children array. I'm trying to pick an item using an Id. const array = [ { uuid: '40E75F3DE56B4B11B3AFBDE46785737B' }, { uuid: '9CEF74766BBB4B9682B7817B43CEAE48' }, { uuid: '34F209A883D3406FBA6BACD9E07DB1D9', children: [{ uuid: 'F429C51BF01C405DA517616E0E16DE4E', children: [{ uuid: '8823CFCE7D4645C68991332091C1A05C' }, { uuid: '58A9345E881F48C980498C7FFB68667D' }] }] }, { uuid: '152488CC33434A8C9CACBC2E06A7E535' }, { uuid: '9152B3DEF40F414BBBC68CACE2F5F6E4' },

Recursive merging of objects in javascript

六月ゝ 毕业季﹏ 提交于 2019-11-28 11:47:35
问题 I'm currently working on a project with react and I'm facing a problem in merging two objects when using setState method. I have two objects similar to person1 = { name: 'ramya', age: 21, gender: 'f', skills:{ technicalSkills: { languages: ['C', 'C++', 'Java'], operatingSystems: ['Unix', 'Linux'] }, linguisticSkills: { toSpeak: ['English', 'Tamil'], toRead: ['English'] } }, } obj2 = { skills: { linguisticSkills: { toRead: ['English', 'Tamil'] } } } I want to change state of person 1's skills

How to compose functions of varying arity using Lodash flow?

孤街醉人 提交于 2019-11-28 11:47:19
I want to do some function composition. I know already this: If f3(x) shall be the same as f1(f2(x)) then f3 = _.flowRight(f1,f2); If f3(x,y) shall be the same as f1(x, f2(y)) then …? (The use case is the composition of node.js/express middleware functions.) In the following images, I use {_} as a placeholder for a value. Think of it as a hole in the code where we pass something in. Ok let's imagine what your function would have to do... Does this seems like a generic transformation? ie, do you think we can use this in many places? – functional programming promotes building functions which are

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

南笙酒味 提交于 2019-11-28 10:54:05
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: "changed description"}) > arr [ {uid: 1, name: "bla", description: "cucu"}, {uid: 2, name: "smth else",

Create chain in lodash with custom functions

戏子无情 提交于 2019-11-28 10:05:42
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 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} _.mixin({ add: add }); var r =_.chain(l).find(function(a){return a>1}).add(5).value() console.log(r); ==> 7

Flatten array with objects into 1 object

时光怂恿深爱的人放手 提交于 2019-11-28 09:38:05
Given input: [{ a: 1 }, { b: 2 }, { c: 3 }] How to return: { a: 1, b: 2, c: 3 } For arrays it's not a problem with lodash but here we have array of objects. Use Object.assign : let merged = Object.assign(...arr); // ES6 (2015) syntax var merged = Object.assign.apply(Object, arr); // ES5 syntax Note that Object.assign is not yet implemented in many environment and you might need to polyfill it (either with core-js, another polyfill or using the polyfill on MDN). You mentioned lodash, so it's worth pointing out it comes with a _.assign function for this purpose that does the same thing: var

Group By and Sum using Underscore/Lodash

老子叫甜甜 提交于 2019-11-28 08:36:59
I have JSON like this: [ { platformId: 1, payout: 15, numOfPeople: 4 }, { platformId: 1, payout: 12, numOfPeople: 3 }, { platformId: 2, payout: 6, numOfPeople: 5 }, { platformId: 2, payout: 10, numOfPeople: 1 }, ] And I want to Group it by platformId with sum of payout and numOfPeople . I.e. in result I want JSON like this: [ "1": { payout: 27, numOfPeople: 7 }, "2": { payout: 16, numOfPeople: 6 } ] I tried to use underscore.js 's _.groupBy method, and it groups fine, but how I can get the SUM of objects properties values like I demonstrated above? You can do this without Underscore: var

lodash - project/transform object into key value array

天大地大妈咪最大 提交于 2019-11-28 07:26:27
I'm about to use forOwn to iterate through an object's properties and create an array manually and can't helping thinking there's a oneliner already available to do it. { prop1 : "value", prop2: { sub:1} } to: [ {key: "prop1", value: "value"}, {key: "prop2", value: {sub:1}} ] Thanks You can use lodash's _.map() with shorthand property names : const obj = { prop1 : "value", prop2: { sub:1} }; const result = _.map(obj, (value, prop) => ({ prop, value })); console.log(result); <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.2/lodash.min.js"></script> Or you can do it using

using lodash to compare arrays (items existence without order)

一笑奈何 提交于 2019-11-28 07:06:52
I know I can do it using loops, but I am trying to find an elegant way of doing this: I have two arrays: var array1 = [['a', 'b'], ['b', 'c']]; var array2 = [['b', 'c'], ['a', 'b']]; I want to use lodash to confirm that the two are the same. By 'the same' I mean that there is no item in array1 that is not contained in array2. In terms of checking equality between these items: ['a', 'b'] == ['b', 'a'] or ['a', 'b'] == ['a', 'b'] both work since the letters will always be in order. Thanks in advance. If you sort the outer array, you can use _.isEqual() since the inner array is already sorted.