0

Are objects with properties the equivalent of associative arrays?

Is it OK if the property names have punctuation characters?

like obj['http://google.com'] = { ... } ?

I can still access it like an array - obj['http://google.com'], but not like an object because of the slashes and stuff.

Basically I’m asking if it's considered a bad practice to use objects like this. If yes, is there any other alternative of getting associative arrays?

2
  • 3
    Yes, objects are sort of the same as associative arrays in languages like PHP. There are two ways to access any property in an object, dot notation or bracket notation. The latter is used if there are special characters in the key or if you need to use a variable, and has nothing to do with arrays. No, it's not considered bad practice to do this, it's the way objects work. Commented Dec 21, 2013 at 21:25
  • ok tks for claryifing that;) Commented Dec 21, 2013 at 21:29

2 Answers 2

3

There's a risk using objects as associative arrays in JavaScript. Consider:

var foo = {};

function putFoo(x, v) {
  foo[x] = v;
}

function getFoo(x) {
  return foo[x];
}

putFoo("a", "blah");
console.log("a", getFoo("a")); // Yes, "a" is there.
console.log("b", getFoo("b")); // No, "b" is not there.
console.log("toString", getFoo("toString")); // what?

Everything is swell until you get to the last console.log. toString is a method in Object.prototype from which object literals get their methods, so you'll get a function out on the console even though you never put anything named toString in the object.

You can work around this by creating your object with Object.create(null). This will create an object that does not have a prototype, and so is not polluted by the members of Object. (Documentation on Object.create.)

Here's a question that explores the performance issues. In brief creating an object with Object.create(null) is more expensive than with {} but accessing the keys on an object created by Object.create(null) is faster than on an object created with {}.

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

Comments

2

are objects with properties the equivalent of associative arrays?

Not exactly, for they are not ordered. They're more like a Map.

is it ok if the property names have punctuation characters, like obj['http://google.com'] = { ... } ?

Yes. As you could see, it works :-)

i can still access it like an array - obj['http://google.com'], but not like an object because of the slashes and stuff.

That's nothing array-specific. If the property name (of any object) is not a valid identifier (because it's a number or does contain a dot) or a dynamic value (like a variable), you have to use bracket notation for property access. In object literals, you can quote the keys.

3 Comments

The terms "associative array" and "Map" are often synonymous. The fact that some quarters have decided to make a distinction between the two does not make the distinction universal.
@Louis: Are they? OK. "Map" always seemed more general to me, while "associative array" indicated some ordering - at least in PHP where the term is most used.
I see where you are coming from but usage varies from subculture to subculture.

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.