1

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 ****/
}
5
  • can't you wait for the next cycle? or do you need inside of the loop an access to the next item? Commented May 23, 2017 at 5:59
  • i need to access 5 items per iteration Commented May 23, 2017 at 6:00
  • you don't need j+=1; since you already have a for loop. When you read, in this case, 3, it'll go to 4 and try to access searchTerms[4] which doesn't exist Commented May 23, 2017 at 6:01
  • @danielgoba84 tell us the requirement Commented May 23, 2017 at 6:02
  • well this is a simplified code, i need to access 5 items per iteration, i use them to create a request, i will also concatenate them to create a unique file name Commented May 23, 2017 at 6:06

5 Answers 5

1

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();

Sign up to request clarification or add additional context in comments.

2 Comments

ok ill edit the code, but that's not the problem, i simplified the JSON, to save space, it's 25 items long, also the code was simplified, (will edit) 'j+=5' and inside the loop i update 'j' and when 'j' is increased and try to access the next element, even in the first iteration it throws the error
@danielgoba84 I added sample code for your case.Please test that
0

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); 
}

Comments

0

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); 
}

1 Comment

do not understand how this should work, can you explain?
0

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.

Comments

0

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.