loop through nested object properties
本问题已经有最佳答案,请猛点这里访问。
我正在尝试检查嵌套对象中是否有空值,如何循环访问嵌套属性?
1 2 3 4 5 6 7 8 9 10 11 | for (var propt in updatedInputs) { if (updatedInputs[propt] =="") this.cancelSubmit(); // check for nested properties if (updatedInputs.hasOwnProperty(propt)) { // loop through nested properties here } } |
样本对象:
1 2 3 4 5 6 7 | Object {contacts: Array[5]} contacts: Array[5] 0:"04354355" 1:"24349878779" 2:"wqewqewqeqw" 3:"wqewqeqwe" 4:"" |
好吧,我想它现在可以做你想做的。
1 2 3 4 5 6 7 8 9 10 | 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]); } } } |