lodash

underscore/lodash unique by multiple properties

狂风中的少年 提交于 2019-11-27 13:36:00
问题 I have an array of objects with duplicates and I'm trying to get a unique listing, where uniqueness is defined by a subset of the properties of the object. For example, {a:"1",b:"1",c:"2"} And I want to ignore c in the uniqueness comparison. I can do something like _.uniq(myArray,function(element) { return element.a + "_" + element+b}); I was hoping I could do _.uniq(myArray,function(element) { return {a:element.a, b:element.b} }); But that doesn't work. Is there something like that I can do,

Lodash : how to do a case insensitive sorting on a collection using orderBy?

[亡魂溺海] 提交于 2019-11-27 13:23:33
问题 I checked this answer but to achieve the same result, that is to get case-insensitive sorting, I need to use orderBy instead of sortBy since it gives the ability to specify the sort order . The only way I found to achieve it was to create a cloned "middle" array mapped to lower case the name : const users = [ { name: 'A', age: 48 }, { name: 'B', age: 34 }, { name: 'b', age: 40 }, { name: 'a', age: 36 } ]; let lowerCaseUsers = _.clone(users); lowerCaseUsers = lowerCaseUsers.map((user) => {

Using Lodash to sum values by key

早过忘川 提交于 2019-11-27 12:28:58
How can I sum the values in objects which share a common key? I need to use Lodash for this because I need good performance if these arrays get huge. var prjMgrValues = [ {"proj_mgr":"Jack ProjManager","submitted_dollars":12000}, {"proj_mgr":"Jack ProjManager","submitted_dollars":750000}, {"proj_mgr":"Joe ProjManager","submitted_dollars":45000} ] I'm looking for an output of [ {"proj_mgr":"Jack ProjManager","submitted_dollars":762000}, {"proj_mgr":"Joe ProjManager","submitted_dollars":45000} ] 4castle This is a case of reduction for each unique element. I always use _.groupBy and then _.map

Error: EACCES: permission denied

不打扰是莪最后的温柔 提交于 2019-11-27 12:17:07
I run npm install lodash but it throws Error: EACCES: permission denied error. I know it is permission issue but as far as I know, sudo permission is not required for installing node module locally. If I run it with sudo, it gets installed inside ~/node_modules folder. drwxrwxr-x is the file permission of existing folder. I can't figure out what might have gone wrong. Below is the error message. npm ERR! tar.unpack untar error /home/rupesh/.npm/lodash/4.13.1/package.tgz npm ERR! Linux 3.13.0-88-generic npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install" "lodash" npm ERR! node v4

How to add mixins to ES6 javascript classes?

有些话、适合烂在心里 提交于 2019-11-27 11:39:46
问题 In an ES6 class with some instance variables and methods, how can you add a mixin to it? I've given an example below, though I don't know if the syntax for the mixin object is correct. class Test { constructor() { this.var1 = 'var1' } method1() { console.log(this.var1) } test() { this.method2() } } var mixin = { var2: 'var2', method2: { console.log(this.var2) } } If I run (new Test()).test() , it will fail because there's no method2 on the class, as it's in the mixin, that's why I need to add

How to remove undefined and null values from an object using lodash?

a 夏天 提交于 2019-11-27 10:42:18
I have a Javascript object like: var my_object = { a:undefined, b:2, c:4, d:undefined }; How to remove all the undefined properties? False attributes should stay. Tx3 If you want to remove all falsey values then the most compact way is: _.pick(obj, _.identity); For example (Lodash 3.x): _.pick({ a: null, b: 1, c: undefined }, _.identity); >> Object {b: 1} For Lodash 4.x: _.pickBy({ a: null, b: 1, c: undefined }, _.identity); >> Object {b: 1} ryeballar You can simply chain _.omit() with _.isUndefined and _.isNull compositions, and get the result with lazy evaluation. Demo var result = _(my

Lodash remove duplicates from array

浪尽此生 提交于 2019-11-27 10:34:48
This is my data: [ { url: 'www.example.com/hello', id: "22" }, { url: 'www.example.com/hello', id: "22" }, { url: 'www.example.com/hello-how-are-you', id: "23" }, { url: 'www.example.com/i-like-cats', id: "24" }, { url: 'www.example.com/i-like-pie', id: "25" } ] With Lodash, how could I remove objects with duplicate id keys? Something with filter, map and unique, but not quite sure. My real data set is much larger and has more keys, but the concept should be the same. ntalbs _.unique no longer work for the current version as lodash 4.0.0 has this breaking change . The functionally was splitted

How to do a deep comparison between 2 objects with lodash?

余生长醉 提交于 2019-11-27 10:26:30
I have 2 nested objects which are different and I need to know if they have difference in one of their nested properties. var a = {}; var b = {}; a.prop1 = 2; a.prop2 = { prop3: 2 }; b.prop1 = 2; b.prop2 = { prop3: 3 }; The object could be much more complex with more nested properties. But this one is a good example. I have the option to use recursive functions or something with lodash... JLavoie An easy and elegant solution is to use _.isEqual , which performs a deep comparison: var a = {}; var b = {}; a.prop1 = 2; a.prop2 = { prop3: 2 }; b.prop1 = 2; b.prop2 = { prop3: 3 }; _.isEqual(a, b);

博客从wordpress迁移到hexo

99封情书 提交于 2019-11-27 09:46:35
参考资料 https://www.jianshu.com/p/fd233d967e88 https://segmentfault.com/a/1190000005624504 https://github.com/theme-next/hexo-theme-next https://zhiho.github.io/2015/09/29/hexo-next/ http://theme-next.iissnan.com/getting-started.html https://blog.csdn.net/q2158798/article/details/82354154 https://www.jianshu.com/p/f068b8a36d84 基础环境 npm [root@wordpross ~]# npm -v 3.10.10 node [root@wordpross ~]# node -v v6.17.1 hexo [root@wordpross ~]# hexo version hexo-cli: 2.0.0 os: Linux 3.10.0-862.14.4.el7.x86_64 linux x64 http_parser: 2.8.0 node: 6.17.1 v8: 5.1.281.111 uv: 1.30.1 zlib: 1.2.7 ares: 1.10.1-DEV

is there a function in lodash to replace matched item

ぃ、小莉子 提交于 2019-11-27 09:42:57
问题 I wonder if there is a simpler method in lodash to replace an item in a JavaScript collection? (Possible duplicate but I did not understand the answer there:) I looked at their documentation but could not find anything My code is: var arr = [{id: 1, name: "Person 1"}, {id:2, name:"Person 2"}]; // Can following code be reduced to something like _.XX(arr, {id:1}, {id:1, name: "New Name"}); _.each(arr, function(a, idx){ if(a.id === 1){ arr[idx] = {id:1, name: "Person New Name"}; return false; }