Convert any iterable of key-value entries into a plain object
APIs often send settings as a list of [key, value] entries, and a Map holds its data the same way. Implement entriesToObject(entries), which turns any iterable of entries into a plain object.
entriesToObject(entries) // => a new plain object
entries can be any iterable: an array, a Map, a Set, a generator, and so on. If it is not iterable (for example null, undefined, a number or a plain object), throw a TypeError.0 is the key and its element 1 is the value. A missing value is undefined, and extra elements are ignored. If an entry is not an array, throw a TypeError.String(), so 1 and '1' are the same key, and an empty entry [] has the key 'undefined'.'__proto__'. The result’s prototype is always Object.prototype, and no other object is affected.entriesToObject([['theme', 'dark'], ['fontSize', 14]]);
// => { theme: 'dark', fontSize: 14 }
entriesToObject(new Map([['en', 'Hello'], ['es', 'Hola']]));
// => { en: 'Hello', es: 'Hola' }
entriesToObject([['a', 1], ['b', 2], ['a', 3]]);
// => { a: 3, b: 2 } (keys in order a, b)
entriesToObject([[7, 'seven'], ['flag']]);
// => { '7': 'seven', flag: undefined }
const settings = entriesToObject([['__proto__', { admin: true }]]);
Object.keys(settings); // => ['__proto__']
settings.admin; // => undefined (the prototype did not change)
entriesToObject([['ok', 1], 'oops']);
// throws TypeError
Do not use Object.fromEntries.