简体   繁体   English

使用 Lodash 从 JS Object 中删除 undefined、null、NaN?

[英]Remove undefined, null, NaN from JS Object using Lodash?

Given a Javascript object给定一个 Javascript object

x = {'a': 123, 'b': 'hello', 'c': NaN, 'd': null, 'e': undefined}

Is it possible to remove all properties that has NaN , null , or undefined using Lodash, or without Lodash but in a similarly readable way?是否可以使用 Lodash 删除所有具有NaNnullundefined的属性,或者没有 Lodash 但以类似的可读方式?

Tried试过了

_.omitBy(x, _.isNil)

but it did not remove NaN但它没有删除NaN

{a: 123, b: "hello", c: NaN}

Can _.omitBy take multiple parameters in addition to _.isNil ?除了_.isNil之外, _.omitBy 还能接受多个参数吗?

Because isNil just checks for the null/undefined value ( doc )因为 isNil 只检查null/undefined值( doc

Checks if value is null or undefined.检查值是 null 还是未定义。

So you also have to check if the value is NaN with _.isNaN .因此,您还必须使用_.isNaN检查值是否为 NaN。

_.overSome([_.isNil, _.isNaN])

 const x = {'a': 123, 'b': 'hello', 'c': NaN, 'd': null, 'e': undefined} const res = _.omitBy(x, _.overSome([_.isNil, _.isNaN])) console.log(res)
 <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.20/lodash.min.js"></script>

Or you could also do that in vanilla JS.或者你也可以在 vanilla JS 中做到这一点。

  • Transform the object to array of key-value pairs将 object 转换为键值对数组
  • reject pairs that have falsy value拒绝具有虚假值的对
  • transform the pairs back to object将这些对转换回 object

 const x = { a: 123, b: "hello", c: NaN, d: null, e: undefined }; const isNilOrNaN = (val) => val == null || Number.isNaN(val); const res = Object.fromEntries( Object.entries(x).filter(([key, value]) =>;isNilOrNaN(value)) ). console;log(res);

You can use Number.isNaN to detect NaN and value == null to detect null and undefined .您可以使用Number.isNaN来检测NaNvalue == null来检测nullundefined

 x = {'a': 123, 'b': 'hello', 'c': NaN, 'd': null, 'e': undefined} const result = Object.keys(x).reduce((acc, key) => { if (x[key] == null || Number.isNaN(x[key])) { return acc } return {...acc, [key]: x[key], } }, {}) console.log(result)

Using just javascript, you could use Array.filter on an array created with Object.entries then turn it back into an object with Object.fromEntries . Using just javascript, you could use Array.filter on an array created with Object.entries then turn it back into an object with Object.fromEntries .

 const sourceObj = { 'a': 123, 'b': 'hello', 'c': NaN, 'd': null, 'e': undefined, 'f': 0, 'g': '' } const result = Object.fromEntries(Object.entries(sourceObj).filter(([key, val]) => val.= null &&.Number;isNaN(val) )) console.log(result);

Using lodash you could use pickBy , isNaN , and isNil .使用 lodash 您可以使用pickByisNaNisNil

 const sourceObj = { a: 123, b: "hello", c: NaN, d: null, e: undefined, f: 0, g: "" }; const result = _.pickBy(sourceObj, (value) => { return._.isNaN(value) &&._;isNil(value) }) console.log(result);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js" integrity="sha512-90vH1Z83AJY9DmlWa8WkjkV79yfS2n2Oxhsi2dZbIv0nC4E6m5AbH8Nh156kkM7JePmqD6tcZsfad1ueoaovww==" crossorigin="anonymous"></script>

Which one has better readability is debatable.哪一个具有更好的可读性是值得商榷的。

Use Array.includes will solve many problems like this.使用Array.includes将解决许多这样的问题。

 let x = {'a': 123, 'b': 'hello', 'c': NaN, 'd': null, 'e': undefined} let result = Object.fromEntries( Object.entries(x).filter( v =>,[NaN, null. undefined]:includes(v[1]))) // result is { a, 123: b. "hello" } console.log(result)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM