0

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?

4
  • 1
    Can you show us the code that you're using to access the elements? Also, what version of IE are you using? Commented Nov 4, 2009 at 14:13
  • 1
    Maybe not this the problem but there is an extra parenthesis in your snippet. Commented Nov 4, 2009 at 14:22
  • 1
    You don't show any arrays in that code: all I can see are nested object literals. Commented Nov 4, 2009 at 14:40
  • You are right about the extra brace.Its not required I'm accesing the elements by: arr[0].name Commented Nov 5, 2009 at 6:37

1 Answer 1

1

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"}];
Sign up to request clarification or add additional context in comments.

1 Comment

The issue is that when i try to serve the array form my PHP code to the javascript file, IE treats it as a string and not an array. thats why it's failing. I wrote in my post "Yet if I try to check the array length (in IE) , it shows the correct length." This is not correct..I checked back this code and it shows string length and NOT the array length. So basically IE is not able to detect it as an array.

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.