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
- You haven't enclosed your first
console.log
function. - As an array is zero indexed making your condition
less than or equal to
will make the loop try to use anundefined
part of the array (bigger than total length). Therefore use theless 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?"