1

I want to use a variable as index of my associative array

var usersName = []; // Just defining
userName[socket.id] = socket.name; // socket.id is an alphanumeric string

I want to use that socket.id(string) as the custom index of usersName array, so I can get a list of all the users connected to the socket. The problem is the way I'm escaping the variable ( I guess).

I've tried this but didn't work:

usersName ['\''+socket.id+'\''] = socket.name;

This works in PHP but, I just can't get it to work in javascript

Thanks for the help.

Mollo
  • 723
  • 3
  • 7
  • 24
  • 2
    Why should javascript work like PHP? For starters, use a `{}`, not `[]`. `userName[socket.id] = socket.name;` will work just fine. – Esailija Mar 23 '13 at 00:03
  • 1
    `userName[socket.id]` sure "works" but not the way you expect. Don't use non-numerical keys with arrays, use plain objects instead. *"The problem is the way I'm escaping the variable ( I guess)."* What made you think there is a problem at all? – Felix Kling Mar 23 '13 at 00:07
  • I'm more like a PHP programmer guy. I forgot to mention that I don't really know the value of `socket.id`(is auto generated for each user connected) if I do what you said. How can I obtain the value of each one ? – Mollo Mar 23 '13 at 00:15
  • 1
    Like so: http://stackoverflow.com/q/85992/218196. – Felix Kling Mar 23 '13 at 00:17
  • 1
    To avoid confusion, use the term [Map](http://en.wikipedia.org/wiki/Associative_array) or Dictionary - or just Object in JavaScript - to refer to a data-structure of Key->Value pairs. The associative array implementation in PHP is, unfortunately, some hybrid mess of a Map and a List - very few languages have such a fundamentally indecisive ADT. JavaScript Arrays, while Objects, have some special characteristics and should generally not be used/confused for a Map. –  Mar 23 '13 at 00:25

2 Answers2

2

What you are trying to do is essentially this:

// say socket = {id: 123, name: 'Bob'};
var foo = {}; // create an object
foo[socket.id] = socket.name; // put socket.name under foo.

var i;
for (i in foo) { //loop through list
   console.log(i, "is", foo[i]); //123 is Bob
}

right? Like the comments posted, JS doesn't have "associative arrays" but instead something better- psuedo-classical objects. Objects in JavaScript are nothing like those in PHP. You need to relearn what an "object" means when learning JavaScript. Arrays in javascript are not something to be proud of.. They are essentially just objects extending the Array prototype and with a special 'length' property. Arrays in JS also allow you to get data in order, by performing a for(i =..; i..; i +=..) loop. Other than the benefits of Array.prototype and the ability to load a list of things in some reliable order, arrays are not that special.

Spencer Lockhart
  • 1,164
  • 7
  • 7
  • 1
    Nit: JavaScript *does* have [Associative Arrays](http://en.wikipedia.org/wiki/Associative_array) - just not the "associative array" implementation associated with PHP ;-) Also, I find Arrays in JavaScript elegant/unified *iff* the programmer keeps the distinction clear and understands what `in` does and the wee bit of magic when assigning to an Array property. –  Mar 23 '13 at 00:30
  • 1
    No, javascript *does not* have associative arrays. The fact that you can do `var arr = []; arr['prop']=value;` does not make arrays associative, the reason being that properties of an array are strictly orderless. Only the indexed elements are ordered. – Beetroot-Beetroot Mar 23 '13 at 01:30
  • Nit 2: JavaScript arrays don't have an order either (because they are just objects). The ordered iteration you get with a `for` loop is because *you* specify which property is accessed in which order, i.e. `i` going from `0` to `n`. You can do the same with any object if you have the property names in the order you want. Arrays just happen to have numerical property names and counting is easy. – Felix Kling Mar 23 '13 at 09:41
  • @Felix Kling, arrays are different than objects with numeric keys. They have a special length property, that is always the value of the highest numeric index. Also, Array.prototype contains several helper functions for arrays. I don't understand the reason for your nit.. I mentioned the way you can grab data from a JS array reliably already. – Spencer Lockhart Mar 23 '13 at 17:19
  • @Spencer: I know that. But that does not make arrays ordered. If you know the length beforehand, you can also use `for` with objects. – Felix Kling Mar 23 '13 at 17:20
  • @Felix Kling, who said they were 'ordered'? – Spencer Lockhart Mar 23 '13 at 17:21
  • You wrote *" Arrays in JS also allow you to get data in order, by performing a for(i =..; i..; i +=..) loop."*. `for` loops don't have anything to do with arrays per se. You can use `for` loops with objects as well. For example, `arguments` is an object, you can easily do `for(var i = 0; i < arguments.length; i++)`. It's not a property of arrays that you can access their elements in order, it's the fact you use the keys in a specific order, as I already explained in my previous comment. – Felix Kling Mar 23 '13 at 17:22
  • That style of for loop was designed for grabbing data from arrays in a reliable order.. Lol. Sure, you can use it for objects, but that nit is just.. I don't see the point, man. – Spencer Lockhart Mar 23 '13 at 17:23
1

Hopefully this will work the way you want it. It's kind of PHP style:

1

var usersName = new Object();
usersName["123"] = 'Jane';
usersName["923"] = 'Charles';

for (var key in usersName) {
    console.log(key + ' => ' + usersName[key]);
}

2

var usersName = {
    '123' : 'Jane',
    '923' : 'Charles'
};

for (var key in usersName) {
    console.log(key + ' => ' + usersName[key]);
}