【问题标题】:What has changed in Lodash from 3 to 4 that this code is not working?Lodash 从 3 到 4 发生了什么变化,这个代码不起作用?
【发布时间】:2016-09-15 04:22:03
【问题描述】:

我有这样的代码:

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 应该是remove empty properties

Lodash 3.10.1 的结果:

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

Lodash 4.15.0 的结果:

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

Lodash 4 发生了哪些变化使其不再工作?

【问题讨论】:

    标签: javascript lodash


    【解决方案1】:

    将您的 const fn = _.partialRight(_.pick, _.identity) 更改为 const fn = _.partialRight(_.pickBy, _.identity);

    _.pick 曾经只是一个功能,但他们在最新更新中将其分解为_.pick_.pickBy。当您传入已知键时,您将使用_.pick,当您使用自定义函数测试是否应根据您自己的参数选择键/值时,您将使用_.pickBy

    https://lodash.com/docs/4.15.0#pick

    https://lodash.com/docs/4.15.0#pickBy

    【讨论】:

      猜你喜欢
      • 2023-02-20
      • 2018-11-12
      • 1970-01-01
      • 1970-01-01
      • 2012-08-29
      • 1970-01-01
      • 1970-01-01
      • 2016-06-09
      相关资源
      最近更新 更多