Object.entries in 2015
The Map constructor takes an iterable of [key, value] pairs. Array.from takes an iterable of elements.This asymmetry is presumably because the Array constructor's behaviour is already defined. A hypothetical Map.from would merely be the same as the Map constructor!
Not quite true: Array.from, unlike every other iterable consumer in
the spec, also takes array-like plain objects: Array.from({0:'E', 1:'S', 2:'6', length:3})
produces ["E","S","6"]
. The motivation for
Map.from is that you could feed it a "map-like object" (that is, an
object with no iterator) and get a Map in return: Map.from({ foo: 1, bar: 2 })
would behave as new Map([['foo',1],['bar',2]])
.
The other motivation might be subclasses - MyArray.from creates a MyArray, MyMap.from would create a MyMap.
Revisiting discussion here, here and there, I'm wondering what the status of Object.entries and Object.values is. Specifically:
new Map(Object.entries(obj))
? (I'm still not sure whether Array.from was intended to set a precedent for futurefrom
methods, or meant solely as ES5 migration sugar).