3

In Javascript, I have an array of objects, users, such that users[1].name would give me the name of that user.

I want to use the ID of that user as the index instead of the ever increasing counter. For example, I can initiate the first user as users[45].

However, I found that once I do users[45], javascript would turn it into a numeric array, such that when I do users.length, I get 46.

Is there anyway to force it to treat the number as string in this case. (" " doesn't work)?

William Sham
  • 12,849
  • 11
  • 50
  • 67
  • You should use objects for associative arrays, but these have no `length` property. You'd have to keep a counter how many users you added (if you need it). The question is, what operations do you want to perform on this data structure? Btw. for an array, `length` will always be the highest index + 1. It does not actually extend the array to this size. – Felix Kling Jul 11 '11 at 23:17
  • 2
    Don't use Arrays as hash maps. Objects are used when a hash map is needed. There's no associative array in JS as there is in PHP. – Ruan Mendes Jul 11 '11 at 23:20
  • Why not arrays for hash maps. Do you mean object of objects should be used instead? – William Sham Jul 11 '11 at 23:25

2 Answers2

11

You cannot use arrays for this sort of function in JavaScript — for more information, see "Javascript Does Not Support Associative Arrays."

Make sure you initialize the users variable as an Object instead. In this object you can store the arbitrary, non-sequential keys.

var users = new Object();

// or, more conveniently:
var users = {};

users[45] = { name: 'John Doe' };

To get the number of users in the object, here's a function stolen from this SO answer:

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

var users = {};
// add users..

alert(Object.size(users));
Community
  • 1
  • 1
Jon Gauthier
  • 25,202
  • 6
  • 63
  • 69
  • 1
    I do believe he has an Array of Objects, not an Object of Objects? Unless you're suggesting to use an Object literal over a Array Literal - in which case you could make that more clear. Just being picky I guess. – Shaz Jul 11 '11 at 23:19
  • 1
    @Shaz: There are no associative arrays in JS, they always be objects. – Dr.Molle Jul 11 '11 at 23:21
  • @Shaz - you're missing the point, he can't use an array of objects in the way he wants, he needs to use an object – tomfumb Jul 11 '11 at 23:22
  • If you added a way to overcome this, (as @Hans has) I would +1 you if it was an answer. – Shaz Jul 11 '11 at 23:23
  • yes. I have in mind an array of object. But object of objects might work too....I just want a "group" of objects, and I need to be able to know how many of them there are – William Sham Jul 11 '11 at 23:25
  • I added an example for counting the number of objects contained in the `users` object. – Jon Gauthier Jul 11 '11 at 23:27
  • That was lighting fast! Thank you. You took off lots of burden from me – William Sham Jul 11 '11 at 23:31
  • `alert(users.size());` should be `alert(Object.size(users));` The size function is not added to the prototype. – Felix Kling Jul 11 '11 at 23:51
  • Oh, missed that. Thanks for the correction; I updated the original. – Jon Gauthier Jul 12 '11 at 00:25
2

Hans has the right answer here. Use keys on an object, not an array. I'd suggest you read these two references:

http://www.quirksmode.org/js/associative.html and http://blog.xkoder.com/2008/07/10/javascript-associative-arrays-demystified/

Trying to make an associative array out of an array object is non-standard and can cause problems (for example .length will be zero). Use keys on an object instead.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • 1
    Thank you. I love responses that refer to articles. They always give me more insight and understanding. Would you happen to have other articles that talks about how to organize arrays for hierarchical data in html, something like an array of array like what i have here – William Sham Jul 11 '11 at 23:31