entries-array
Returns an array of the key-value pairs of an Object, Map, Array, or Typed Array. Useful for when you need the entries of a collection object but aren’t sure what type of collection you’ll be given.
Installation
Requires Node.js 7.0.0 or above.
npm i entries-array
API
The module exports a single function.
Parameters
- Bindable:
c
(Array, iterator, Object, Map, Set, string, or Typed Array) - Optional: Object argument:
arrays
/maps
/sets
(arrays of classes/strings): Arrays of classes and/or string names of classes that should be treated as equivalent toArray
/Map
/Set
(respectively).detectPairs
(boolean): This option only has an effect whenc
is an Array of two-item pairs, such as[[1, 2], [3, 4]]
. WhendetectPairs
is set tofalse
(the default behavior), the Array indexes will be used as the keys (the returned array will be[[0, [1, 2]], [1, [3, 4]]]
). But ifdetectPairs
istrue
, the module will interpret the first item in each pair as the entry key (a copy of the original array will be returned:[[1, 2], [3, 4]]
).inObj
(boolean): Whether or not to act like the “in” operator by including inherited Object properties. Only takes effect ifc
is an Object (i.e. not another recognized type). Defaults tofalse
.reflectObj
(boolean): Whether or not to include non-enumerable Object properties by using reflection. Only takes effect ifc
is an Object (i.e. not another recognized type). Defaults tofalse
.reverse
(boolean): Iftrue
, then entries are returned in reverse order. Defaults tofalse
.
Return Value
An array of two-element key-value pair arrays.
Examples
Arrays
const entries = // [[0, 'a'], [1, 'b']] // Supports the bind operator'a' 'b':: // [[0, 'a'], [1, 'b']]
Examples using an array of entries (key/value pairs):
const entries = const arr = 'key1' 'val1' 'key2' 'val2' // Default behavior without the `detectPairs` option // [[0, ['key1', 'val1']], [1, ['key2', 'val2']]] // With `detectPairs` set to `true` // [['key1', 'val1'], ['key2', 'val2']]
Objects
const entries = // [['key', 'value']] // Supports the bind operatorconst obj = key: 'value'obj:: // [['key', 'value']]
Inherited Object Properties
Include Object properties from the prototype chain by setting inObj
to true
:
const entries = {}Clsprototypekey = 'value' // [['key', 'value']]
Non-Enumerable Object Properties
Include non-enumerable Object properties by setting reflectObj
to true
:
const entries = const obj = {}Object // [['key', 'value']]
Iterators
entries-array
will treat an iterator as if it were an array of values. Each “key” will be an incrementing integer index that starts at zero.
const entries = { 'a' 'b'} // [[0, 'a'], [1, 'b']]
Maps
const entries = const map = map // [['key', 'value']]
Sets
entries-array
will treat a Set like an array, and will add integer index keys starting at zero. Note that this behavior is different from that of the built-in Set.prototype.entries()
method.
const entries = const set = setset // [[0, 'first'], [1, 'second']]
Strings
entries-array
will treat a string like a character array.
const entries = // [[0, 'h'], [1, 'i']]
Typed Arrays
const entries = const typedArray = 4 // [[0, 0]]