由于某些原因,我需要一个函数将具有null属性的数组内的对象转换为具有空对象属性的对象。
该函数应该是递归的,并且必须适用于任何对象(深层嵌套,数组中的数组,对象中的数组等等)
有一个我需要的例子:
var obj = {
"parent" : [
null,
{"child" : 2},
{"child" : 3}
],
"parent2" : {
"parent3" : [
{"child" : 1},
null,
{"child" : 3}
]
},
"parent4" : [
{"child" : [
{"childa" : 1},
{"childa" : 2},
null
]
},
{"child" : [
{"childa" : 1},
null,
null
]
},
null
]
}
然后:
var obj2 = nullToEmptyObject(obj);
obj2应该是这样的:
{
"parent" : [
{},
{"child" : 2},
{"child" : 3}
],
"parent2" : {
"parent3" : [
{"child" : 1},
{},
{"child" : 3}
]
},
"parent4" : [
{"child" : [
{"childa" : 1},
{"childa" : 2},
{}
]
},
{"child" : [
{"childa" : 1},
{},
{}
]
},
{}
]
}
我还没有做任何事,因为我不知道如何递归。如果你能给我一个起始代码,我可以完成。
谢谢你,如果我的英语不好,请编辑我!
答案 0 :(得分:1)
你可以尝试这样的事情:
function nullToEmptyObject(obj){
var obj2 = {}:
for (var k in obj)
{
if (obj[k] === null)
obj2[k] = {};
else if (typeof obj[k] == "object")
obj2[k] = nullToEmptyObject(obj[k]);
else if (Array.isArray(obj[k])){
obj2[k] = []
for (i=0; i<obj[k].length; i++) {
obj2.push(nullToEmptyObject(obj[k][i]);
}
}
}
return obj2;
}
该函数循环遍历所有对象属性并根据需要更改它们:
答案 1 :(得分:1)
您可以首先使用递归来检查元素是否为object并使用for-in
循环并检查每个元素是否为null
或保持循环,然后检查具有forEach
的数组并重复相同测试。任何时候函数找到null
它将被更改为{}
var obj = {"parent":[null,{"child":2},{"child":3}],"parent2":{"parent3":[{"child":1},null,{"child":3}]},"parent4":[{"child":[{"childa":1},{"childa":2},null]},{"child":[{"childa":1},null,null]},null]}
function nullToEmptyObject(el) {
//Check for object not array and loop with for-in. If the returned value is null change to object else keep looping with recursion
if (typeof el === 'object' && !Array.isArray(el)) {
for (var p in el) {
(el[p] !== null) ? nullToEmptyObject(el[p]): el[p] = {}
}
//Check for Array and loop with forEach. If the returned value is null change to object else keep looping with recursion .
} else if (Array.isArray(el)) {
el.forEach(function(e, i) {
(e !== null) ? nullToEmptyObject(e): el[i] = {}
})
}
}
nullToEmptyObject(obj);
console.log(JSON.stringify(obj, 0, 4))
&#13;
这里的测试是更复杂的数据示例,在对象的第一个parent
属性中有更多嵌套元素
var obj = {
"parent": [
null, [null, {
'random': null,
'moreRandom': {
'moreRandom': {
'moreRandom': null,
'random': [null, null]
}
}
}], {
"child": 2
}, {
"child": 3
}
],
"parent2": {
"parent3": [{
"child": 1
},
null, {
"child": 3
}
]
},
"parent4": [{
"child": [{
"childa": 1
}, {
"childa": 2
},
null
]
}, {
"child": [{
"childa": 1
},
null,
null
]
},
null
]
}
function nullToEmptyObject(el) {
//Check for object not array and loop with for-in. If the returned value is null change to object else keep looping with recursion
if (typeof el === 'object' && !Array.isArray(el)) {
for (var p in el) {
(el[p] !== null) ? nullToEmptyObject(el[p]): el[p] = {}
}
//Check for Array and loop with forEach. If the returned value is null change to object else keep looping with recursion .
} else if (Array.isArray(el)) {
el.forEach(function(e, i) {
(e !== null) ? nullToEmptyObject(e): el[i] = {}
})
}
}
nullToEmptyObject(obj);
console.log(JSON.stringify(obj, 0, 4))
&#13;
答案 2 :(得分:0)
再次回忆一下当前的功能,因为多个数据里面的所有内容都是对象,甚至你需要检查只有空...
function nullToEmptyObject(o){
for(var i in o){
if(o[i] == null){
o[i] = {};
}
else{
nullToEmptyObject(o[i]);
}
}
}
nullToEmptyObject(obj);
console.log(JSON.stringify(obj,0,4));