I have JSON file, which i load in js code without problems using JSON.parse() (A reduced example of the file shown here, it's 25 items long):
{
"List": [
{ "Term": "Tos" },
{ "Term": "Gripa" },
{ "Term": "Catarro"},
{ "Term": "Flemas"}
]
}
When I iterate it accessing one item per iteration I have no problems, but when i try to increase the index to access to items per iteration it throws the following error (Comment in code shows the line with the problem):
console.log(searchTerms[j].Term);
TypeError: Cannot read property 'Term' of undefined
var data = fs.readFileSync(searchTermsFile);
var searchTerms = JSON.parse(data);
searchTerms = searchTerms.List;
for(var j=0;j<searchTerms.length;j+=4)
{
console.log(searchTerms[j].Term);
j+=1;
console.log(searchTerms[j].Term); /****<---- THIS LINE THROWS THE ERROR ****/
}
j+=1;since you already have a for loop. When you read, in this case, 3, it'll go to 4 and try to accesssearchTerms[4]which doesn't exist