0

I thought I solved my problem using the for (var key in arr) suggested here but this causes trouble in IE. Now I am back to square one.

var myVariable = [];

myVariable['option-1'] = 'something';
myVariable['option-2'] = 'something else';
$.each(myVariable, function(index, value) {
  alert(index + ': ' + value);
});

It doesn't work. Nothing shows. Can someone edit it to make it work?

1
  • Array methods don't support named properties. Use an Object ({}) ( not an Array ([]) ) and a for ... in loop Commented May 1, 2013 at 20:21

5 Answers 5

4

Just use an object instead of an array:

var myVariable = {};
Sign up to request clarification or add additional context in comments.

5 Comments

it is confusing when code works perfectly fine in two browsers (FF and Chrome) and not at all in another (IE). With the change you suggested even IE seems to understand my code. Tnx
That's interesting, you may have bumped into a jQuery bug; I'll look into it. In any case, you should always use an object if you need named keys.
IE added elements that were not there. If the object/array had two elements myVariable['options-2'] = 3 and myVariable['options-5'] = 1. Then in IE it would have a third element myVariable['indexOf'] the value was a whole function.
@RST My IE skills are a little rusty. Do you mean IE has indexOf as an enumerable property on Array.prototype? Only if we're talking about IE<=8, right? Newer IE versions have native support to that method, and it should not be enumerable (I hope they didn't screw that up).
With for...in...{alert(key)...} would show 2 alerts in FF/Chrome and 3 alerts in IE. It was IE8 comp.mode., which seems to be IE7
3

Change var myVariable = []; to var myVariable = {};.

The syntax that you're using, myVariable['option-1'] = 'something'; is for objects, not arrays.

Comments

2

myVariable is an array. To add things to an array you use myVariable.push('something'). You are using square bracket syntax, which is how you would normally add properties to an object.

Since arrays are objects, you can still access option-1, but it is not a member of the array, it is a property of the object: myVariable['option-1']; // "something"

The solution is to set myVariable to an object. jQuery's each method will iterate over the properties of the object as expected.

Comments

1

No need for jQuery.
Just use a for..in loop (with an object {} not an array []):

for(var index in myVariable) {
   var value = myVariable[index];
   alert(index + ': ' + value);
}

Comments

1

There are no associative arrays in JavaScript, and non-numeric properties on arrays are bad practise!.

Instead, use an object literal:

var myVariable = {};
//               ^^

myVariable['option-1'] = 'something';
…

Comments

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.