admin管理员组文章数量:1357568
Given a 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?
Tried
_.omitBy(x, _.isNil)
but it did not remove NaN
{a: 123, b: "hello", c: NaN}
Can _.omitBy take multiple parameters in addition to _.isNil
?
Given a 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?
Tried
_.omitBy(x, _.isNil)
but it did not remove NaN
{a: 123, b: "hello", c: NaN}
Can _.omitBy take multiple parameters in addition to _.isNil
?
5 Answers
Reset to default 4Because isNil just checks for the null/undefined
value (doc)
Checks if value is null or undefined.
So you also have to check if the value is NaN with _.isNaN
.
_.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/npm/[email protected]/lodash.min.js"></script>
Or you could also do that in vanilla JS.
- Transform the object to array of key-value pairs
- reject pairs that have falsy value
- transform the pairs back to 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
.
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
.
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
.
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./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.
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)
I did it like this:
const x = {'a': 123, 'b': 'hello', 'c': NaN, 'd': null, 'e': undefined}
const cleaned = _(x).omitBy(_.isNil).omitBy(_.isNaN).value()
console.log(cleaned)
<script src="https://cdn.jsdelivr/npm/[email protected]/lodash.min.js"></script>
本文标签: javascriptRemove undefinedNULLNaN from JS Object using LodashStack Overflow
版权声明:本文标题:javascript - Remove undefined, null, NaN from JS Object using Lodash? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744018117a2576711.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论