I have some JSON data that I'm trying to extract.
I'm trying to extract and reformat some of the data to an array. I'm looping through the data, but am having problems extracting the data within a nested submeaning object.
JSON data:
var data = [
{
"meaning": "a procedure intended to establish the quality, performance, or reliability of something, especially before it is taken into widespread use.",
"examples": [
"no sparking was visible during the tests"
],
"submeanings": [
{
"meaning": "a short written or spoken examination of a person's proficiency or knowledge.",
"examples": [
"a spelling test"
]
},
{
"meaning": "an event or situation that reveals the strength or quality of someone or something by putting them under strain.",
"examples": [
"this is the first serious test of the peace agreement"
]
},
{
"meaning": "an examination of part of the body or a body fluid for medical purposes, especially by means of a chemical or mechanical procedure rather than simple inspection.",
"examples": [
"a test for HIV",
"eye tests"
]
},
{
"meaning": "a procedure employed to identify a substance or to reveal the presence or absence of a constituent within a substance."
}
]
},
{
"meaning": "a movable hearth in a reverberating furnace, used for separating gold or silver from lead."
}
]
Algorithm:
// array to hold definitions
var definitions = [];
for (var i = 0; i < data.length; i++) {
// push first
definitions.push(data[i]['meaning']);
// push second, if submeaning data exists
if (data[i]['submeanings'].length >= 1) {
definitions.push(data[i]['submeanings'][i]['meaning']);
}
}
When I run this code, I'm getting the following error:
Uncaught TypeError: Cannot read property 'length' of undefined(…)
Any help is appreciated.