↧
Answer by Yair Levy for How to convert a plain object into an ES6 Map?
ES6convert 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...
View ArticleAnswer by Andrew Willems for How to convert a plain object into an ES6 Map?
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...
View ArticleAnswer by Ohar for How to convert a plain object into an ES6 Map?
const myMap = new Map( Object .keys(myObj) .map( key => [key, myObj[key]] ))
View ArticleAnswer by Maurits Rijk for How to convert a plain object into an ES6 Map?
Alternatively you can use the lodash toPairs method:const _ = require('lodash');const map = new Map(_.toPairs({foo: 'bar'}));
View ArticleAnswer by Bergi for How to convert a plain object into an ES6 Map?
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...
View ArticleAnswer by nils for How to convert a plain object into an ES6 Map?
Yes, 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');...
View ArticleHow to convert a plain object into an ES6 Map?
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...
View Article
More Pages to Explore .....