Reputation: 577
I have created a JSON object, Now when i am trying to print the data from JSON object, I am getting no output on document.write
and undefined
Message on Console.
Kindly check my code and guide me where i am making the Mistake.
<head>
<script>
var variable=
[
{
"first_name": "Steve",
"last_name": "Jobs",
"Education" : [
{"Intermediate" : "2006" },
{"Bachelors" : "2009" },
{"Masters" : "2015" }
]
},
{
"first_name": "Bill",
"last_name": "Gates",
"Education": [
{"Intermediate" : "2010"},
{"Bachelors" : "2012"},
{"Masters" : "2014"}
]
}
];
console.log(variable.first_name);
for (key in variable.Education)
{
console.log(key);
}
</script>
</head>
Upvotes: 0
Views: 185
Reputation: 20418
Try this way
for (var i in variable) {
var k = variable[i].Education;
console.log(Object.keys(variable[i])) // displays all keys
for (var j in k)
console.log(Object.keys(k[j])) // displays Education keys
}
Upvotes: 0
Reputation: 872
your "variable" is an array so yon can't show a proprety of an element until define correctly where is the proprety.
as :
console.log(variable[0].Education)
or doing a loop :
for (var i = 0; i <variable.length; i++){
console.log(variable[i].Education);
}
Upvotes: 2