2

data is an array of Json data The structure of each object is:

var data = [
{
    id: 0, 
    img: "image_src", 
    width: 107, 
    height: 80, 
    shadowBoxLink: "....",
    th: {
        width: 107,
        height: 70, 
        img: "src"
    }
},
{
    id: 1, 
    img: "image_src", 
    width: 107, 
    height: 80, 
    shadowBoxLink: "....",
    th: {
        width: 107,
        height: 80, 
        img: "src"
    }
}
];

When I try to access the array in a loop (only happens in IE8, IE7) with:

for(var i in data) {
    var imgHeight = data[i].th.height;
}

I got an error message: "Impossible to get property of "height" the reference is null or not defined"

(I translated the message from french: Impossible d’obtenir la propriété « height » d’une référence null ou non définie)

What am I doing wrong?

1
  • See edit for another possible issue. Commented Mar 20, 2013 at 11:40

3 Answers 3

8

Accessing array elements can be done more semantically like this:

for(var i = 0, n = data.length; i < n; i ++) {
    var imgHeight = data[i].th.height;
    ...
}

for..in loops are meant to be used with key-based objects.

NOTE: you also have a missing closing quote in your object:

th: Object {
   width: 107,
   height: 80, 
   img: "src /* NEED A CLOSING " HERE */
}
Sign up to request clarification or add additional context in comments.

4 Comments

@RobW The structure elaborated in the question is the structure of each object in the array (data); i.e., data is an array of objects.
Thx @Abody97, the quote I just forgot to put it here but it exists in the original code
I tried the loop you suggested, no errors, but nothing is displayed as well, I'm trying to debug it to see if I have a problem somewhere else
@vladimire I'm thinking it is a bug somewhere else, since the errors went away.
2

It seems you're looking for the property somewhere it doesn't exist

Make a simple test:

for(var i in data) {
  if(data[i] && data[i].th && data[i].th.height){
    console.log('the property exists');
  }else{
    console.log("no, it doesn't")
  }      
}

4 Comments

I don't think the issue is with the object itself, since the asker said he's only getting the error in IE.
your approach is correct, but here the problem is different. Please check @Abody97's answer
I tried you approach, I get 'the property exists' except for the last one ! it's like there is an an empty entry in the array
Well, yes @Abody97 answer says it all. Since you're using for in IE is iterating not only the arrays content but other properties as well and they don't have th.height ofcourse. So use for() loop
0

There is an array of objects.

So, use for and get the required object's property.

There is a syntax error in the given code. Close the string with quotes.

Example code is here.

var data = [
    {
        id: 0, 
        img: "image_src1", 
        width: 107, 
        height: 80, 
        shadowBoxLink: "....",
        th: {
            width: 107,
            height: 70, 
            img: "src"
        }
    },
    {
        id: 1, 
        img: "image_src2", 
        width: 107, 
        height: 80, 
        shadowBoxLink: "....",
        th: {
            width: 107,
            height: 40, 
            img: "src"
        }
    }
];

for(var i=0; i<data.length; i++) {
    var imgHeight = data[i].th.height;
    alert(imgHeight);
}            

1 Comment

but i'm trying to access the property "height" inside th ?

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.