Annibigi
Annibigi

Reputation: 6045

Javascript array object fails throwing "object error" in IE

I have a javascript array object like

{arr = {"0":{"name":"henry","role":"user"},"1":{"name":"mark","role":"admin"}}

I have a html which requests this array from my server( by including a script tag with url to my server function that serves this array}

The problem is that When I run my HTML file in firefox, it easily detects this array and does the further processing with it but On IE , when I try to access the elements of the array, it throws an exception (object is null or undefined).

Any idea why element detection could be failing in IE.

How do we find out the properties of an array object in IE?

Upvotes: 0

Views: 1261

Answers (1)

Christian C. Salvadó
Christian C. Salvadó

Reputation: 827266

That is not really an array, is simply an object literal with numeric properties, you should iterate it by using the for...in statement:

var arr = {"0":{"name":"henry","role":"user"},
           "1":{"name":"mark","role":"admin"}};

for (var key in arr) {
  if (arr.hasOwnProperty(key)) {
    // value = arr[key];
  }
}

But I think you should simply return a real Array:

var arr = [{"name":"henry","role":"user"},
           {"name":"mark","role":"admin"}];

Upvotes: 1

Related Questions