I have JSON data in this format:
{
"head": {
"heading": ["header1", "header2", "header3"]
},
"body": {
"elements": [{
"header1": {
"value": "value1"
},
"header2": {
"value": "value2"
}
"header3": {
"value": "value3"
}
},
{
"header1": {
"value": "value1"
},
"header2": {
"value": "value2"
}
"header3": {
"value": "value3"
}
}
]
}
I can't predict the number of values in headings. So, I initially parsed the values in headings and stored in an array. Later while printing the body elements. I wrote the following code
// data has the parsed json file
var heading = [];
data.head.heading.forEach(function(head) {
heading.push(head);
});
data.body.elements.forEach( function(elem) {
for (var head in the heading) {
var header = heading[head];
alert(elem.header.value); //not accessing value inside header
}
});
On using values.header.value it needs to access value inside header1, header2 and header3 of each elem but it is returning an error
TypeError: elem.header is undefined
It is taking the value as the header instead of value inside the variable header. Is there any way to make it access the values inside the variable header? thanks in advance
header1, 2, 3. You'll need to run troughObject.keys.