循环遍历嵌套对象属性

时间:2016-02-03 18:58:23

标签: javascript

我正在尝试检查嵌套对象中的空值,我将如何遍历嵌套属性?

for (var propt in updatedInputs) {

  if (updatedInputs[propt] == "") this.cancelSubmit();

  // check for nested properties
  if (updatedInputs.hasOwnProperty(propt)) {

    // loop through nested properties here

  }
}

示例对象:

Object {contacts: Array[5]}
    contacts: Array[5]
    0: "04354355"
    1: "24349878779"
    2: "wqewqewqeqw"
    3: "wqewqeqwe"
    4: ""

1 个答案:

答案 0 :(得分:1)

很好,我认为它现在可以做你想做的事。

function iterObj(obj) {

  for (var key in obj) {
    console.log(key + ': ' + obj[key]);
    if (obj[key] !== null && typeof obj[key] === "object") {
      // Recurse into children
      iterObj(obj[key]);
    }
  }
}