Problem : I need to convert an object to a map to get the values of key dynamically

// My Object :

v:Voiture = {"id":1 , "marque": "peogeot" }

Solution : 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'

If you need to support older environments and transpilation is not an option for you, use a polyfill


Object.entries = typeof Object.entries === 'function' ? Object.entries : obj => Object.keys(obj).map(k => [k, obj[k]]);

Laisser un commentaire