Javascript null为空对象属性

时间:2016-06-28 14:13:09

标签: javascript

由于某些原因,我需要一个函数将具有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},
         {},
         {}
       ]
     }, 
     {}
  ]
}

我还没有做任何事,因为我不知道如何递归。如果你能给我一个起始代码,我可以完成。

谢谢你,如果我的英语不好,请编辑我!

3 个答案:

答案 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. 当属性为null时,它设置为{}
  2. 当属性为对象时,它将在树中下降
  3. 当属性是数组时,它会循环遍历它

答案 1 :(得分:1)

您可以首先使用递归来检查元素是否为object并使用for-in循环并检查每个元素是否为null或保持循环,然后检查具有forEach的数组并重复相同测试。任何时候函数找到null它将被更改为{}

&#13;
&#13;
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;
&#13;
&#13;

这里的测试是更复杂的数据示例,在对象的第一个parent属性中有更多嵌套元素

&#13;
&#13;
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;
&#13;
&#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));