Reputation: 13
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 ****/
}
Upvotes: 1
Views: 504
Reputation: 2719
Your object contains 4 Term
and your for
loop count to 4 but j+=1
in last iteration make j=5
and there is no object in searchTerms[5]
.This is why your code doesn't work.
I write sample program.I hope this helps you:
var data = {
"List": [
{"Term": "1"},
{"Term": "2"},
{"Term": "3"},
{"Term": "4"},
{"Term": "5"},
{"Term": "6"},
{"Term": "7"},
{"Term": "8"},
{"Term": "9"},
{"Term": "10"},
{"Term": "11"},
{"Term": "12"}
]
};
function test() {
var searchTerms = data.List;
var j = 0;
var currentFiveObject = [];
for (j = 0; j < searchTerms.length; j += 5) {
currentFiveObject = [];
for (var i = 0; i < 5; i++) {
if (j + i < searchTerms.length)
currentFiveObject.push(searchTerms[j + i])
else
break;
}
console.log(currentFiveObject);
}
}
test();
Upvotes: 1
Reputation: 386600
You could use a default object, which resolves the error, but returns undefined
as well.
j += 1;
console.log((searchTerms[j] || {}).Term);
The other way to deal with indices out of range, is to use a check in advance and prevent from using an index greater than the last index value.
if (j < searchTerms.length) {
// process item
console.log(searchTerms[j].Term);
}
Upvotes: 0
Reputation: 11
The real problem you're facing is that your j-index is requiring an item that's not contained in your JSON object. I found no reason for your cycle to increase your index variable in the middle of logging your Terms.
If that index increment is intended, then you need to consider if the value of j is equal or larger than searchTerms length before using it as the index accessor.
Remove it or deal with it.
Upvotes: 0
Reputation: 820
You can try this
var data = fs.readFileSync(searchTermsFile);
var searchTerms = JSON.parse(data);
var i = 0;
searchTerms = searchTerms.List;for(var j=0;j<searchTerms.length;j++)
{
i = j
console.log((searchTerms[i] || {}).Term);
i +=1;
console.log((searchTerms[i] || {}).Term);
}
Upvotes: 0
Reputation: 281726
Thats because you are incrementing j
and then checking , however since you are iterating till the length - 1
it will go out of bounds after j += 1
, try
var data = fs.readFileSync(searchTermsFile);
var searchTerms = JSON.parse(data);
searchTerms = searchTerms.List;for(var j=0;j<searchTerms.length - 1;j++)
{
console.log(searchTerms[j].Term);
j+=1;
console.log(searchTerms[j].Term);
}
Upvotes: 0