0

I have an object that associates a key to an array of values. Eg.

var map = {
    "600": [
        "600",
        "0"
    ],
    "602": [
        "600",
        "0"
    ],
    "6723": [
        "6646",
        "600",
        "0"
    ],
    "6646": [
        "600",
        "0"
    ]
}

I noticed that Javascript will resolve a key when using an array for key lookup.

Eg.

map[600] => ["600","0"]
map[[600]] => ["600","0"]
map[[[600]]] => ["600","0"]

Curious about this inherent input flattening? Also when you pass multi-value array, JS will pick that last argument. But if nest arrays it returns undefined.

Eg.

map[600, 6723] => ["6646","600","0"]
map[[600, 6723]] => undefined

Any insights/links/docs for what is happening here? Thanks!

  • [Specification](//tc39.es/ecma262/#sec-property-accessors-runtime-semantics-evaluation), [documentation](//developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/Property_Accessors#property_names). Object properties are either symbols or strings. Anything else is coerced to a string. What `map[[600, 6723]]` results in should be easy to find out if you know what `String([600, 6723])` is. – Sebastian Simon Dec 20 '21 at 20:42

0 Answers0