从JavaScript对象中递归删除空值 [英] Recursively remove null values from JavaScript object
问题描述
我有一个JSON obj,经过一些操作(比如删除一些部分),我打印它,一切看起来不错,除了我有一些 null
值。如何删除这些?
I have a JSON obj, after some operations (like delete some pieces), I print it and everything looks good except that I have some null
values. How do I remove these?
我使用 JSON.stringify(obj,null,2)
方法进行打印,以下是它的样子:
I use JSON.stringify(obj, null, 2)
method to print, and here is what it looks like:
{
"store": {
"book": [
null,
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
null,
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
null,
"price": 19.95
}
}
}
我希望它紧凑而漂亮clean(删除额外的3 null
值):
I want it to be much compact and pretty clean(remove the extra 3 null
values):
{
"store": {
"book": [
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
推荐答案
我必须解决类似的问题,但是我不仅要删除 null 值,还要删除未定义, NaN ,空字符串,为空通过检查嵌套对象和嵌套数组,递归地返回数组和空对象值。
I had to solve a similar problem, however I wanted to remove not only null values but also undefined, NaN, empty String, empty array and empty object values, recursively, by inspecting nested objects and also nested arrays.
以下函数正在使用Lo-短划线:
The following function is using Lo-Dash:
function pruneEmpty(obj) {
return function prune(current) {
_.forOwn(current, function (value, key) {
if (_.isUndefined(value) || _.isNull(value) || _.isNaN(value) ||
(_.isString(value) && _.isEmpty(value)) ||
(_.isObject(value) && _.isEmpty(prune(value)))) {
delete current[key];
}
});
// remove any leftover undefined values from the delete
// operation on an array
if (_.isArray(current)) _.pull(current, undefined);
return current;
}(_.cloneDeep(obj)); // Do not modify the original object, create a clone instead
}
例如,如果使用以下输入对象调用该方法:
For example, if you invoke the method with the following input object:
var dirty = {
key1: 'AAA',
key2: {
key21: 'BBB'
},
key3: {
key31: true,
key32: false
},
key4: {
key41: undefined,
key42: null,
key43: [],
key44: {},
key45: {
key451: NaN,
key452: {
key4521: {}
},
key453: [ {foo: {}, bar:''}, NaN, null, undefined ]
},
key46: ''
},
key5: {
key51: 1,
key52: ' ',
key53: [1, '2', {}, []],
key54: [{ foo: { bar: true, baz: null }}, { foo: { bar: '', baz: 0 }}]
},
key6: function () {}
};
它会以递归方式丢弃所有坏值,最后只保留那些值一些信息。
It'll recursively discard all the "bad" values, keeping in the end only the ones that carry some information.
var clean = pruneEmpty(dirty);
console.log(JSON.stringify(clean, null, 2));
{
key1: 'AAA',
key2: {
key21: 'BBB'
},
key3: {
key31: true,
key32: false
},
key5: {
key51: 1,
key52: ' ',
key53: [1, '2'],
key54: [{ foo: { bar: true }}, { foo: { baz: 0 }}]
}
};
希望有所帮助!
这篇关于从JavaScript对象中递归删除空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!