从对象中删除未定义的属性 [英] Remove undefined properties from object
本文介绍了从对象中删除未定义的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想递归地清理任何未定义的字段,或者有一个空对象作为父对象,如下所示:
示例:
<预><代码>{一种": {乙":{c":未定义,}},d":未定义,e":{f:未定义,},G": {你好',j":未定义,},"k": "L",米":{n":{o":{z":未定义,}},"p": 'toto'}}我期待以下结果:
<预><代码>{G": {你好',},"k": 'L',米":{"p": 'toto'}}感谢您的帮助
解决方案
function cleanArray(actual) {var newArray = new Object();newArray=JSON.parse(JSON.stringify(actual));返回新数组;}变量对象={一种": {乙":{c":未定义,}},d":未定义,e":{f:未定义,},G": {你好',j":未定义,},"k": "L",米":{n":{o":{z":未定义,}},"p": 'toto'}};console.log(cleanArray(obj));
I would like to recursively clean any fields which are undefined, or have an empty object as parent like this following:
Example:
{
"a": {
"b": {
"c": undefined,
}
},
"d": undefined,
"e": {
f: undefined,
},
"g": {
"h": 'I',
"j": undefined,
},
"k": "L",
"m": {
"n": {
"o": {
"z": undefined,
}
},
"p": 'toto'
}
}
I am expecting the following result :
{
"g": {
"h": 'I',
},
"k": 'L',
"m": {
"p": 'toto'
}
}
Thank you for your help
解决方案
function cleanArray(actual) {
var newArray = new Object();
newArray=JSON.parse(JSON.stringify(actual));
return newArray;
}
var obj={
"a": {
"b": {
"c": undefined,
}
},
"d": undefined,
"e": {
f: undefined,
},
"g": {
"h": 'I',
"j": undefined,
},
"k": "L",
"m": {
"n": {
"o": {
"z": undefined,
}
},
"p": 'toto'
}
};
console.log(cleanArray(obj));
这篇关于从对象中删除未定义的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文