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.

5
  • 2
    Why should javascript work like PHP? For starters, use a {}, not []. userName[socket.id] = socket.name; will work just fine. Commented Mar 23, 2013 at 0: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? Commented Mar 23, 2013 at 0: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 ? Commented Mar 23, 2013 at 0:15
  • 1
    Like so: stackoverflow.com/q/85992/218196. Commented Mar 23, 2013 at 0:17
  • 1
    To avoid confusion, use the term Map 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. Commented Mar 23, 2013 at 0:25

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Nit: JavaScript does have Associative Arrays - 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.
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.
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, 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: I know that. But that does not make arrays ordered. If you know the length beforehand, you can also use for with objects.
|
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]);
}

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.