-1

I've looked around to see whether Javascript supports associative arrays and despite the fact that it clearly says that it doesn't, it seems to work anyway?

I'd love to know if anything has changed in the specifications and whether or not it's recommended to use them.

Sample code:

var foo = new Array;
foo["bar"] = "It works";

console.log(foo["bar"]);
RagZ
  • 149
  • 1
  • 1
  • 6
  • You can use objects instead... like `var foo = {};` – Arun P Johny Oct 19 '15 at 06:43
  • 4
    An array is an object. You're taking advantage of that and adding object properties to the array. You shouldn't do that. You're looking for an actual object `{}`, not an array. – m59 Oct 19 '15 at 06:44
  • @ArunPJohny I know I can use objects, but I didn't ask for an alternative. – RagZ Oct 19 '15 at 06:45
  • 1
    Also, please stop using `document.write()`. – m59 Oct 19 '15 at 06:45
  • @m59 Haha, sure. do you have a nice alternative for small code examples like that? – RagZ Oct 19 '15 at 06:47
  • 3
    How about `console.log`? – Kos Oct 19 '15 at 06:47
  • @m59 And about your answer, that's what I wanted to know! thank you – RagZ Oct 19 '15 at 06:48
  • @Kos I wanted to quickly get something visual, but eh, sure. – RagZ Oct 19 '15 at 06:49
  • 1
    console.log is ideal (it's visual). Sometimes `alert` is useful for quick debugging on a mobile device if you don't have a better option available. I use `document.body.textContent = 'stuff'` sometimes. Sure, `document.write()` works (in your case), but you should hate it enough not to want to use it anyway. – m59 Oct 19 '15 at 06:50
  • If these answers helped you, accept one of them. –  Oct 24 '15 at 13:55

3 Answers3

1

You should use objects for that, they are first-class citizens in JavaScript:

var foo = {
    "bar": "It works"
};

console.log(foo.bar);
console.log(foo["bar"]);
slomek
  • 4,873
  • 3
  • 17
  • 16
  • 2
    I was tempted to downvote this just because it contains `document.write`. Please either correct the OP about that (preferable) or remove it from the post. Noobies see that kind of thing all over the place and think they should be doing it too. – m59 Oct 19 '15 at 06:46
  • I was just trying to show that RagZ can use it the same way he/she wanted to do in his/her own snippet. – slomek Oct 19 '15 at 06:49
0

In Javascript, foo["bar"] is semantically equivalent to foo.bar. But this is Javascript object, not associative array. As you can see, however, you can treat the object as associative array.

LeleDumbo
  • 9,192
  • 4
  • 24
  • 38
0

I've looked around to see whether Javascript supports associative arrays and despite the fact that it clearly says that it doesn't

Where did you see that it clearly stated that JavaScript does not support associative arrays?

Associative arrays--a collection of key/value pairs (also called maps, dictionaries, and many other names in various contexts)--are the fundamental datatype in Javascript. However, in JavaScript, they are called "objects".

However, given your sample code:

var foo = new Array;
foo["bar"] = "It works";

it seems you may be asking a slightly different question, which is "Can a JavaScript array contain properties (other than its numerically indexed ones)"?

The answer, as well documented, and covered in countless question here on SO, is "yes". The reason is that in JavaScript, arrays are a special type of object, so like all objects, they can have named properties.

whether or not it's recommended to use them.

If you mean is it recommended to hang named properties off a JavaScript array, opinions differ. You can take a look at this question.

Community
  • 1
  • 1
  • To answer your question about where it says that it doesn't support associative array, https://www.w3schools.com/js/js_arrays.asp states, "JavaScript does not support associative arrays". – DevonTaig Aug 20 '18 at 17:37