Package Exports
- array-keyed-map
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (array-keyed-map) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
array-keyed-map

A map where the keys are arrays of arbitrary values. Works with any JavaScript value. Uses the actual identity of the key values, not some fragile string-serialisation hack.
Example
const m = arrayKeyedMap()
m.set(['a', 'b'], 1)
m.set(['a', 'b', true], 2)
m.set(['a', ''], 3)
m.set(['a'], 4)
m.get(['a', 'b']) => 1
m.get(['a', 'b', true]) => 2
m.get(['a', '']) => 3
m.get(['a']) => 4
You can use arbitrary JavaScript values as array elements and as values.
Non-primitive key-array elements are treated by identity, like Map
does.
API
Construct an array-keyed map object:
const arrayKeyedMap = require('array-keyed-map')
const akmap = arrayKeyedMap()
The constructor takes no arguments.
akmap.set(array, value)
Arguments:
array
:Array
of valuesvalue
: any value
Sets the value for the given array.
Objects in the array are treated by identity. The identity of the array object itself is irrelevant.
Returns undefined
.
akmap.has(array)
Arguments:
array
:Array
of values
Returns a Boolean: whether a previously set value exists for that key array.
akmap.get(array)
Arguments:
array
:Array
of values
Returns the previously assigned value for this array, or undefined
otherwise.
akmap.delete(array)
Arguments:
array
:Array
of values
Deletes the value at this exact array. Does not affect other array, even if they are prefixes or extensions of this one. Remember to do this if you no longer need a array: the keys and values are not automatically garbage-collected, even if the objects used as keys go out of scope!
Returns undefined
.
FAQ
Why is this better than .join('/')
ing the keys and using a regular object?
Because your key array's elements might have
/
s in them. For example, the arrays['a/b']
and['a', 'b']
would both resolve to the keya/b
.So use something other than a
/
? Sure, but then you have the same problem with elements possibly containing that.So use a sufficiently long probabilistically unguessable string like
03f2a8291a700b95904190583dba17c4ae1bf3bdfc2834391d60985ac6724940
? That wastes RAM/disk when you have many long arrays. And also what the heck are you doing, that's illegal.Because even an empty
Object
has built-in properties on it (e.g.length
, orgetOwnPropertyNames
), which your keys might accidentally overwrite, causing subtle bugs down the line.Because you might want your key array to contain objects (by identity) rather than strings. Objects are difficult or impossible to stringify (e.g. they may contain cyclic references), and even then two distinct objects with identical contents would stringify to the same value and cause subtle bugs.
What version of JS does this rely on?
ES2015βit uses Map
s and
Symbol
s (β caniuse
links). At time of writing, it works in any recent Node.js or browser. Except
IE, of course.
Does this implement the full Map
API?
No. See related issue. I'd take a PR though! π
License
ISC.