Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've create an array in Javascript designed to hold basic details about a set of upgrades as part of a game I'm making. The upgrades are defined as follows;

var Upgrade = {Name:NameIn,
               Description:DescriptionIn,
               Image:ImageIn,
               Purchased:0,
               Visible:false,
               ID:IDIn}

I've got a standard for loop that is trying to access these variables. Yet for some reason the Description variable comes up with an "undefined" error when I try to access it using the i variable I've created as part of the loop. i.e. Upgrades[i].Description comes up as undefined.

I've checked and all of the other elements such as Name, Image etc can be accessed using the "i" variable. e.g. Upgrades[i].Name

Yet if I manually type in what i is equal to as part of my code at the time I try to access Description (e.g having Upgrades[1] as oppose to Upgrades[i]) it allows access to it.

The array is filled as such;

function AddUpgrade(NameIn,DescriptionIn,ImageIn,IDIn){
var Upgrade = {Name:NameIn,
               Description:DescriptionIn,
               Image:ImageIn,
               Purchased:0,
               Visible:false,
               ID:IDIn}
Upgrades.push(Upgrade);

AddUpgrade("Sharpened Rocks","Description",
        "img/upgrades/Caveman2.png","Caveman2Display");

Below is the loop that is trying to access the variables to send to an HTML document;

for (i = 1; i < Upgrades.length ; i++){
    if (Upgrades[i].Visible == 1){
        document.getElementById("upgrades").innerHTML += "<div class=\"upgrade\" id=\"" + Upgrades[i].ID + 
        "\" onclick=\"ButtonBuyUpgrade(" + i + ")\"><img src=\"" + Upgrades[i].Image + "\"><div class=\"hovertext\" id=\"" 
        + Upgrades[i].ID + "Hover\">" + DisplayUpgradeCost(i) + Upgrades[i].Description + "</div></div>";
    }
}

Here is the output from console;

Uncaught TypeError: Cannot read property 'Description' of undefined inc.js:161 DisplayUpgrade inc.js:161 CalculateUnlocks inc.js:463 (anonymous function) inc.js:502 Object {Name: "Sharpened Rocks", Description: "Description", Image: "img/upgrades/Caveman2.png", Purchased: 0, Visible: 1…} Description: "Description" ID: "Caveman2Display" Image: "img/upgrades/Caveman2.png" Name: "Sharpened Rocks" Purchased: 0 Visible: 1

And here is the console.dir output;

1: Object Description: "Description" ID: "Caveman2Display" Image: "img/upgrades/Caveman2.png" Name: "Sharpened Rocks" Purchased: 0 Visible: 1

All the other elements are also filled in, there are no missing pieces of information.

I'm completely confused by this and have no idea why it's happening. Any thoughts?

share|improve this question
    
There are 2 for loops: for(;;) and for( in ). Which are you using. The object you posted here might well end up having a member called Description with the value undefined. It all depends on what the value of DescriptionIn was when the object was created... –  Elias Van Ootegem Aug 20 '14 at 12:00
    
Try console.log(Upgrades) or console.log(Upgrades[i]) in your loop. –  parchment Aug 20 '14 at 12:00
    
Can we see how you fill your array? –  mrgnou Aug 20 '14 at 12:02
    
The error means that you are trying to access an element at an index that doesn't exist. console.dir your array before the loop and see if there are any holes. –  Felix Kling Aug 20 '14 at 14:49

2 Answers 2

Looks like you've created an object. An object is a collection of key:value pairs. An array is a collection of elements ordered by index (starting at 0), denoted by []. To loop through your object try forIn loop:

var Upgrade = {
  Name:'NameIn',
  Description:'DescriptionIn',
  Image:'ImageIn',
  Purchased:0,
  Visible:'false',
  ID:'IDIn'
}

for(var i in Upgrade) {
  console.log(Upgrade[i])
} 
share|improve this answer
    
I need to access specific variables at certain points. It is effectively an array of objects, and I need to access the variables out of sequence of the object variable order. –  Mitch Douglas Aug 20 '14 at 14:36
up vote 0 down vote accepted

To close this one, it turns out the issue was due to the scope of the i variable in my loop coming from another loop the function was being called by, so the program was getting confused. I solved the issue by having the loop say (var i = 0 to create a local variable called i to use.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.