var formmd = {
"frmType": "Registration",
"frmStage": "step1-complete",
"formattr": {
"SystemUser": {
"LoginName": "[email protected]",
"Password": "password",
"PIN": "",
"IsTestUser": false
},
"ConsumerAddress": {
"AddressLine1": "201 MOUNT Road",
"AddressLine2": null,
"AddressTypeId": "1",
"City": "OLA TRAP",
"State": "NM",
"Zipcode": "60005"
},
"ConsumerPhone": {
"PhoneTypeId": 6,
"PhoneNumber": "",
"PrimaryPhoneIndicator": null,
"AllowVoicemail": false
},
"PhysicianSpecialty": {
"SpecialtyList": [
"1",
"2"
]
},
}
}
我想獲取formattr下的子對象的所有值,但我無法迭代子對象內。以下是我嘗試的腳本。 我的結果應該是迭代通過子對象,並獲取所有的值與JavaScript
"[email protected]"
"password"
"PIN": ""
False
201 MOUNT Road"
我試過的腳本是
function walk(formmd) {
var obj1 = formmd.formattr;
for(var key in obj1){
if (obj1.hasOwnProperty(key)) {
var val1 = obj1[key];
if(val1.hasOwnProperty(key)){
for(var key in val1){
var val2 =val1[key];
console.log("val2");
}
}
}
}
}
如何訪問以自動化的方式子對象的鑰匙?
提示:什麼是'如果(val1.hasOwnProperty(鍵)){'部分在做什麼? – nnnnnn
兩次使用'key'似乎不是一個好主意。如果您真的想重新使用該變量名稱,請使用'let'而不是'var'來封鎖它的範圍。無論如何,爲了調試這個,爲什麼不用一個調試器遍歷它,在每一步中檢查變量值? – 2017-02-28 02:56:50