admin管理员组文章数量:1129459
For some reason I can't find this simple thing in the MDN docs (maybe I'm just missing it).
I expected this to work:
const map = new Map({foo: 'bar'});
map.get('foo'); // 'bar'
...but the first line throws TypeError: (var)[Symbol.iterator] is not a function
How do I make a Map from a plain object? Do I really have to first convert it into an array of arrays of key-value pairs?
For some reason I can't find this simple thing in the MDN docs (maybe I'm just missing it).
I expected this to work:
const map = new Map({foo: 'bar'});
map.get('foo'); // 'bar'
...but the first line throws TypeError: (var)[Symbol.iterator] is not a function
How do I make a Map from a plain object? Do I really have to first convert it into an array of arrays of key-value pairs?
Share Improve this question asked Apr 15, 2016 at 10:18 callumcallum 37.6k38 gold badges112 silver badges175 bronze badges 1 |6 Answers
Reset to default 392Yes, the Map
constructor takes an array of key-value pairs.
Object.entries
is a new Object static method available in ES2017 (19.1.2.5).
const map = new Map(Object.entries({foo: 'bar'}));
map.get('foo'); // 'bar'
It's currently implemented in Firefox 46+ and Edge 14+ and newer versions of Chrome
If you need to support older environments and transpilation is not an option for you, use a polyfill, such as the one recommended by georg:
Object.entries = typeof Object.entries === 'function' ? Object.entries : obj => Object.keys(obj).map(k => [k, obj[k]]);
Do I really have to first convert it into an array of arrays of key-value pairs?
No, an iterator of key-value pair arrays is enough. You can use the following to avoid creating the intermediate array:
function* entries(obj) {
for (let key in obj)
yield [key, obj[key]];
}
const map = new Map(entries({foo: 'bar'}));
map.get('foo'); // 'bar'
The answer by Nils describes how to convert objects to maps, which I found very useful. However, the OP was also wondering where this information is in the MDN docs. While it may not have been there when the question was originally asked, it is now on the MDN page for Object.entries() under the heading Converting an Object to a Map which states:
Converting an Object to a Map
The
new Map()
constructor accepts an iterable ofentries
. WithObject.entries
, you can easily convert fromObject
toMap
:const obj = { foo: 'bar', baz: 42 }; const map = new Map(Object.entries(obj)); console.log(map); // Map { foo: "bar", baz: 42 }
ES6
convert object to map:
const objToMap = (o) => new Map(Object.entries(o));
convert map to object:
const mapToObj = (m) => [...m].reduce( (o,v)=>{ o[v[0]] = v[1]; return o; },{} )
Note: the mapToObj function assumes map keys are strings (will fail otherwise)
const myMap = new Map(
Object
.keys(myObj)
.map(
key => [key, myObj[key]]
)
)
Alternatively you can use the lodash toPairs method:
const _ = require('lodash');
const map = new Map(_.toPairs({foo: 'bar'}));
本文标签: javascriptHow to convert a plain object into an ES6 MapStack Overflow
版权声明:本文标题:javascript - How to convert a plain object into an ES6 Map? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736744521a1950698.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Object.entries
really is the better approach overObject.keys
, and bergi's generator function approach is slightly more direct than eitherObject.keys
orObject.entries
. – T.J. Crowder Commented Jul 11, 2018 at 12:30