admin管理员组文章数量:1321429
I'm trying to move underscore to lodash. But this line of code baffles me.
On my current project we have this line of code.
obj = _.pick(obj, _.identity);
Which is pretty obvious that it's trying to delete empty property.
Now when I switch to lodash, the same line of code returns empty object for me.
I'm trying to figure why. How do I achieve the same effect in lodash?
I tried this on both lodash and underscore websites. They produce different results.
This is from lodash
var obj = {_v:'10.1', uIP:'10.0.0.0', _ts:'123'}
_.pick(obj, _.identity);
Object {}
This is from underscore
var obj = {_v:'10.1', uIP:'10.0.0.0', _ts:'123'}
_.pick(obj, _.identity);
Object {_v: "10.1", uIP: "10.0.0.0", _ts: "123"}
I'm trying to move underscore to lodash. But this line of code baffles me.
On my current project we have this line of code.
obj = _.pick(obj, _.identity);
Which is pretty obvious that it's trying to delete empty property.
Now when I switch to lodash, the same line of code returns empty object for me.
I'm trying to figure why. How do I achieve the same effect in lodash?
I tried this on both lodash and underscore websites. They produce different results.
This is from lodash
var obj = {_v:'10.1', uIP:'10.0.0.0', _ts:'123'}
_.pick(obj, _.identity);
Object {}
This is from underscore
var obj = {_v:'10.1', uIP:'10.0.0.0', _ts:'123'}
_.pick(obj, _.identity);
Object {_v: "10.1", uIP: "10.0.0.0", _ts: "123"}
Share
Improve this question
asked Mar 23, 2016 at 21:41
toytoy
12.2k29 gold badges101 silver badges180 bronze badges
3
- Have a look at the docs: lodash./docs#pick (and notice the method that es after that). – Felix Kling Commented Mar 23, 2016 at 21:44
- Oh, that's it. I thought lodash is patible with underscore. If you can put that as the answer. Thanks a lot. – toy Commented Mar 23, 2016 at 21:45
- felix is a beast, so fast.... – omarjmh Commented Mar 23, 2016 at 21:52
2 Answers
Reset to default 6Why _.pick(object, _.identity) in lodash returns empty Object?
Because pick
in lodash expects an array of property names to be passed to it:
var object = { 'a': 1, 'b': '2', 'c': 3 };
_.pick(object, ['a', 'c']);
// → { 'a': 1, 'c': 3 }
How do I achieve the same effect in lodash?
Lodash has a method called pickBy
which accepts a callback function:
var object = { 'a': 1, 'b': '2', 'c': 3 };
_.pickBy(object, _.isNumber);
// → { 'a': 1, 'c': 3 }
I had the same issue, lodash has a slightly different name for this method than underscore:
var object = { 'a': 1, 'b': '2', 'c': 3 };
_.pickBy(object, _.isNumber);
// → { 'a': 1, 'c': 3 }
本文标签: javascriptWhy pick(objectidentity) in lodash returns empty ObjectStack Overflow
版权声明:本文标题:javascript - Why _.pick(object, _.identity) in lodash returns empty Object? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742102536a2420867.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论