Error while trying to iterate JSON array in JavaScript

danielgoba84

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 ****/
}
Yashar Aliabbasi

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

"Exception evaluating SpringEL expression" error while trying to iterate List(<Object>) in Thymeleaf Spring Boot

Golang interface conversion error when trying to iterate through map of json array

Validation error while trying to parse a json array to List[Object] in Scala

Error while trying to destructure an array of possible length 2

How to iterate over array with whole numbers, while adding fractions - Javascript

Iterate over a json Object in javascript having an array inside array

Getting error while trying to render array of object in react js

Error while trying to load a JSON object with python 3

iterate through complex nested json array javascript

Error:-too many values to unpack (expected 2), while trying to iterate over two columns in a Data Frame

Trying to iterate an array of objects in angular

Trying to iterate over JSON in Pug but keep getting length error

JSON error in Java while trying to access google api

Getting Error : lvalue required while trying to implement a Jagged Array in C

error while parsing Json in javascript

JavaScript throwing error while trying to overwrite file

AttributeError error while trying to split an array of numbers

Error while trying to Iterate over List of Strings

Error while trying to parse json into R

Getting error while trying to match two array using PHP

How to get iterate JSON object and JSON array using JavaScript .forEach

error while trying to parse JSON values

Error while trying to open a new window with JavaScript

Error while trying to phrase JSON with Swift

Error while trying to read from a Json file in c#

Trying to iterate over array

Error while trying to store an array to dataframe cell

Undefined error while trying to map over array of objects React Native

Error Trying to access array offset while activating WordPress debug