我以两种不同的方式输出一个对象的长度,这会导致两种不同的响应:
console.log(appData.HomeGateway.questions.length);
console.log(appData.HomeGateway.questions);
输出:
3
0: Object
1: Object
2: Object
3: Object
4: Object
5: Object
6: Object
7: Object
8: Object
length: 9
知道为什么我会得到两种不同的长度吗? appData.questions 似乎出于某种原因从其他对象继承了对象。
这是 JSON:
var appData = {
"HomeGateway": {
"company": "",
"title": {
"english": "",
"spanish": "",
"russian": "",
"otherlanguage": ""
},
"language": "english",
"pressOKQuestion": {
"left": {
"english": "Press ",
"spansih": "",
"russian": ""
},
"right": {
"english": " to move to the next question",
"spanish": "\" to move to the next question",
"russian": ""
}
},
"pressOKMessage": {
"left": {
"english": "Press ",
"spansih": "",
"russian": ""
},
"right": {
"english": " to move on",
"spanish": "\" to move to the next question",
"russian": ""
}
},
"pressUpDown": {
"left": {
"english": "Please press ",
"spanish": "",
"russian": ""
},
"middle": {
"english": " or ",
"spanish": "",
"russian": ""
},
"right": {
"english": " on your remote control to select",
"spanish": "",
"russian": ""
}
},
"questions": [{
"url": "/question/1",
"question": {
"english": "Have you talked to your doctor today?",
"spanish": "",
"russian": ""
},
"view": "views/question.html",
"controller": "loadData",
"nextPage": "#/question/2",
"answers": [{
"id": 1,
"answer": {
"english": "Yes",
"spanish": "Si",
"russian": ""
}
}, {
"answer": {
"english": "No",
"spanish": "No",
"russian": ""
},
"message": "/message/3"
}]
}, {
"url": "/question/2",
"question": {
"english": "On a scale of 0-10, how bad is your pain today?",
"spanish": "",
"russian": ""
},
"icon": "img/icons/1446585749_Doctor_Consultation.png",
"iconSrc": "Broken Arm",
"view": "views/question.html",
"controller": "loadData",
"nextPage": "#/question/3",
"answers": [{
"answer": {
"english": "Low",
"spanish": "",
"russian": ""
},
"category": "1-2"
}, {
"answer": {
"english": "Medium",
"spanish": "",
"russian": ""
},
"category": "3-4"
}, {
"answer": {
"english": "High",
"spanish": "",
"russian": ""
},
"category": "5-8",
"message": "/message/4"
}, {
"answer": {
"english": "Very High",
"spanish": "",
"russian": ""
},
"category": "9-10",
"message": "/message/4"
}]
}, {
"url": "/question/3",
"question": {
"english": "Please enter your systolic blood pressure.",
"spanish": "",
"russian": ""
},
"icon": "img/icons/1446585795_Blood_Pressure_Kit.png",
"iconSrc": "Blood Pressure",
"view": "views/toggle-question.html",
"controller": "loadData",
"nextPage": "#/finished",
"toggle": {
"value": 100,
"max": 500,
"min": 50,
"high": {
"value": 150,
"message": "/message/5"
},
"low": {
"value": 75,
"message": "/message/6"
}
}
}],
"messages": [{
"url": "/message/1",
"message": {
"english": "Please talk to your nurse or doctor about why you do not want to.",
"spanish": "",
"russian": ""
},
"icon": "img/alert-red.png",
"iconSrc": "Alert",
"view": "views/message.html",
"controller": "loadData",
"nextPage": "#/message/2"
}, {
"url": "/message/2",
"message": {
"english": "Good job!",
"spanish": "",
"russian": ""
},
"icon": "img/icons/1446586860_Best_Choice.png",
"iconSrc": "Thumbs Up!",
"view": "views/message.html",
"controller": "loadData",
"nextPage": "#/question/1"
}, {
"url": "/home",
"title": {
"english": "Thank you for being part of today.",
"spanish": "",
"russian": ""
},
"subtitle": {
"english": "Remember this device does not take place of a personal emergency response system or a call to 911.",
"spanish": "",
"russian": ""
},
"view": "views/home.html",
"controller": "loadData",
"nextPage": "#/question/1"
}]
}
}
注意:只是对其进行了编辑以反射(reflect)我用来获取不同长度的方法。
最佳答案
我认为您在第二个 console.log 中跳过了一个键
假设这不是实际代码,而是为我们编写的,我假设您错过了第二个控制台中的问题键。它可能看起来像这样
console.log(appData.HomeGateway.questions.length)
console.log(appData.HomeGateway)
关于javascript - 对象长度不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33532479/