问题描述
我有这样的代码:
const _ = require('lodash');
const fn = _.partialRight(_.pick, _.identity);
const x = { some: 'value', empty: null };
const y = fn(x);
console.log('x:', x);
console.log('y:', y);
fn
应该删除空属性
Lodash 3.10.1的结果:
Result with Lodash 3.10.1:
x: { some: 'value', empty: null }
y: { some: 'value' }
Lodash 4.15.0的结果:
Result with Lodash 4.15.0:
x: { some: 'value', empty: null }
y: {}
在Lodash 4中,什么不再起作用了?
What has changed in Lodash 4 that it's not working anymore?
推荐答案
将您的const fn = _.partialRight(_.pick, _.identity)
更改为const fn = _.partialRight(_.pickBy, _.identity);
_.pick
过去只是一个功能,但在最新更新中将其分解为_.pick
和_.pickBy
.您将在传递已知键时使用_.pick
,而在使用自定义函数来测试是否应根据自己的参数选择键/值时使用_.pickBy
_.pick
used to be just one function but they broke it out into _.pick
and _.pickBy
in the latest updates. you would use _.pick
when you are passing in known keys and _.pickBy
when you are using a custom function to test if a key/value should be picked based on your own parameters,
https://lodash.com/docs/4.15.0#pick
https://lodash.com/docs/4.15.0#pickBy
这篇关于Lodash从3变为4,导致该代码无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!