Reputation: 755
I output the length of an object in two different ways and it leads to two different responses:
console.log(appData.HomeGateway.questions.length);
console.log(appData.HomeGateway.questions);
Outputs:
3
0: Object
1: Object
2: Object
3: Object
4: Object
5: Object
6: Object
7: Object
8: Object
length: 9
Any idea why I'm getting two different lengths? The appData.questions appears to be inheriting objects from other objects for whatever reason.
Here's the 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"
}]
}
}
Note: Just editted it to reflect the method I'm using to get the different lengths.
Upvotes: 1
Views: 47
Reputation: 25341
There is no appData.questions
. It must be appData.HomeGateway.questions
which gives three in both cases (of course it does). And here is a demo of your own data:
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"
}]
}
};
document.write(appData.HomeGateway.questions.length);
document.write("<br/>");
document.write(appData.HomeGateway.questions);
console.log(appData.HomeGateway.questions.length);
console.log(appData.HomeGateway.questions);
Upvotes: 1
Reputation: 2678
I think you are skipping a key in the second console.log
Assuming this is not actual code, but written for us, I am assuming you missed the questions key in your second console. It may look like this
console.log(appData.HomeGateway.questions.length)
console.log(appData.HomeGateway)
Upvotes: 1