3

I am trying to set up an array in jQuery and I then need to do a for loop on it. But it seems that I cant use an associative array for some reason?

var items = new Array();
items['foo'] = 123456;
items['bar'] = 789012;
items['baz'] = 345678;
items['bat'] = 901234;
alert(items.length);

This is just a test, but it return 0?

thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
Sinnbeck
  • 617
  • 4
  • 20
  • 56

4 Answers4

11

You can't make associative array in JavaScript like what you want, instead you can use Object.

For example:

var items = {
  foo : 123456,
  bar : 789012,
  baz : 345678,
  bat : 901234
}

And to calculate the length you can do:

var getObjectSize = function(obj) {
    var len = 0, key;
    for (key in obj) {
        if (obj.hasOwnProperty(key)) len++;
    }
    return len;
};

Use: getObjectSize(items); // output: 4

For more see here.

Another one is:

Object.keys(items).length;

But not supported by all browsers.

Community
  • 1
  • 1
thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
  • 1
    JavaScript objects are also associative arrays. I'm sure of it, I use this flexibility all the time. – Diego Aug 07 '12 at 17:47
  • Yep, check out this thread: http://stackoverflow.com/questions/5223/length-of-javascript-object-ie-associative-array – Andrew Mao Aug 07 '12 at 17:49
  • Technically, no -- JavaScript associative arrays are objects that happen to behave like associative arrays. But as I've said on this subject before, if it walks like a duck and quacks like a duck, just call it a duck. – Blazemonger Aug 07 '12 at 17:51
  • @Diego: In languages that don't thoroughly conflate "array" and "dictionary" (i'm looking at you, PHP!), an "array" is a sequence of contiguous elements indexed by number and a "dictionary" is a key->value map. JS's only likeness between them is that "array" indexes are object properties, and an object is a "dictionary". Anything that's not numeric is not an array element; it's an object property. – cHao Aug 07 '12 at 18:05
2
var items = new Array();
items['foo'] = 123456;

The problem lies in the very first line. You believe that you are adding an item to the array at the index foo, but you are actually adding a property to the items variable with a key foo and value 123456. If you were to type items.foo it would give you back your 123456.

The problem with this approach is that adding a property to an array does not magically increase it's length.

If you want to have non-numeric indexes, you need to use an object instead of an array:

var items = {
    foo: 123456,
    bar: 789012,
    baz: 345678,
    bat: 901234
};
jbabey
  • 45,965
  • 12
  • 71
  • 94
2

Another approach might be to set up two different arrays, which you construct in parallel:

var items = [], items2 = [];
items.push('foo');
items2.push(123456);
// etc.
alert(items2.length);​

The efficiency of this approach depends on how you'll use it. If you're only going to loop through the list of items and do something to each of them, this approach may be more efficient. But if you need to use it like an associative array (items['foo']), then you're better off building an object.

Blazemonger
  • 90,923
  • 26
  • 142
  • 180
  • `items = items2 = []` points `items` and `items2` at the exact same array, making the length double what it should be due to its having both the keys and values. – cHao Aug 07 '12 at 17:56
  • Whoops, I was mixing my JavaScript and PHP and creating something entirely new in the process. Fixed. – Blazemonger Aug 07 '12 at 17:59
1

The .length property returns the highest numerical index of the array. Thus, in your case, there is no numerical index and it returns 0. Try

items[98] = "something";

items.length will be 98..! Use the .length property with caution, and if you also want to count the non-numerical indici, loop over the Object (an Array is also an Object) and count its ownProperties.

Willem Mulder
  • 12,974
  • 3
  • 37
  • 62