1

Below is the response from the backend :

results = [{
    "name": "FantasticBeasts",
    "count": 546809,
  },

  {
    "name": "SSIPOS",
    "count": 16257,
  },

  {
    "name": "xyz",
    "count": 16257,
  }
]

for (var i = 0; i <= results.length; i++) {
  console.log('result ==>', results[i]);
}

My end goal is to fetch the name from every element of the array. But before that, I need to iterate over the array elements. But I am unable to iterate through the array elements.

result ==> undefined

2
  • 4
    Condition should be i < results.length; as index starts from 0 Commented Jan 18, 2019 at 8:26
  • The error is like @Satpal says: You might do this error again in the future, but someday the syntax of iterating over an array is that common to your mind that it is ideomatic and you will do it automatically in the correct way Commented Jan 18, 2019 at 8:35

5 Answers 5

4

It should be i < results.length because with a <= you're comparing an additional non existing value.

results = [{
    "name": "FantasticBeasts",
    "count": 546809,
  },

  {
    "name": "SSIPOS",
    "count": 16257,
  },

  {
    "name": "xyz",
    "count": 16257,
  }
]

for (var i = 0; i < results.length; i++) {
  console.log('result ==>', results[i]);
}

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

3 Comments

the output is displaying with every character in the result for example: result ==> { result ==> " result ==> n .....
@Aditya and what output do you actually expect? This is how the console prints the results.
@Aditya this answer solves the problem (I have also upvoted it), but using a forEach is more advisable as discussed in my answer.
4

In javascript arrays are 0 indexed so, there are 0 to length-1 indexes. So we need to run loop accordingly from 0 to < length or <= length-1

results = [{
    "name": "FantasticBeasts",
    "count": 546809,
  },

  {
    "name": "SSIPOS",
    "count": 16257,
  },

  {
    "name": "xyz",
    "count": 16257,
  }
]

for (var i = 0; i < results.length; i++) {
  console.log("Name:", results[i].name, "Count:", results[i].count);
}

results.forEach(function(result) {
  console.log("Name:", result.name, "Count:", result.count);
});

forEach is bit more cleaner way to achieve the same result.

Reference : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

Comments

2

You iterate from 0 to array.length-1. So you have to replace:

i <= results.length;

by:

i < results.length;

Comments

1

Using a forEach is more efficient in this case, as it's more testable and gives you more control without having to manually access the index.

results.forEach(r => console.log(`result ==> ${r.name}`));

Comments

0

You can also have a look at .forEach and then passing by reference the function (no parameters).

results.forEach(console.log);

This will console log each Result.

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.