Does the complex object / array use as expected key work?

I need a set of "complex things" where the "complex thing" is an array of numbers or strings.

Can a simple object be used for this?

Example:

var set = {}; set[[1,2]] = 1; set[[1,2]] = 1; set[["string", "another string"]] = 1; set[["string", "another string"]] = 1; 

Now, I expect set have two key / value pairs, and testing in Chrome confirms that it is. Can you rely on this behavior?

+4
source share
1 answer

Not at all.

Object keys can only be strings or numbers.
Complex objects will be converted to strings by calling toString() .

You can see this in spec :

Let propertyNameString be ToString (propertyNameValue).

Therefore, set[ [1,2] ] same as set["1,2"] .

+2
source

Source: https://habr.com/ru/post/1395658/


All Articles