What has changed in Lodash from 3 to 4 that this code is not working?

半世苍凉 提交于 2019-12-11 02:37:17

问题


I have a code like this:

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 is supposed to remove empty properties

Result with Lodash 3.10.1:

x: { some: 'value', empty: null }
y: { some: 'value' }

Result with Lodash 4.15.0:

x: { some: 'value', empty: null }
y: {}

What has changed in Lodash 4 that it's not working anymore?


回答1:


change your const fn = _.partialRight(_.pick, _.identity) to const fn = _.partialRight(_.pickBy, _.identity);

_.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



来源:https://stackoverflow.com/questions/39503069/what-has-changed-in-lodash-from-3-to-4-that-this-code-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!