Skip to content Skip to sidebar Skip to footer

Why Am I Getting TypeError: Array[i] Is Undefined?

So in my program, I have an array that contains a dictionary/hash of values and when I loop through the array, I get the value I need but any code after the for loop does not get e

Solution 1:

Issues

  1. You haven't enclosed your first console.log function.
  2. As an array is zero indexed making your condition less than or equal to will make the loop try to use an undefined part of the array (bigger than total length). Therefore use the less than operator.

Fixed code

var array = [
  {"name": "a", "pos": "C"},
  {"name": "b", "pos": "B"},
  {"name": "c", "pos": "W"},
];


for(var i = 0; i < array.length; i++) {
  console.log(array[i]['pos']);
}

console.log("some other code");

Post a Comment for "Why Am I Getting TypeError: Array[i] Is Undefined?"