4 ms·
ASK: Did Ecmascript 2017 screw up Object.entries()?
// keys
Object.keys(key => {})
// values
Object.values(value => {})
// * * entries * *
Object.entries(weirdArray => {
key = weirdArray[0];
value = weirdArray[1];
})
Why not:
Object.entries(entry => {
key = entry.key;
value = entry.value;
})
Or:
Object.entries((key, value) => {})
- wakeywakeywakey 8y agoYou can use array destructing, when supported: Object.entries(([k, v]) => { })
- wakeywakeywakey 8y agoTyped that on my phone, should be: Object.entries({}).forEach(([k, v]) => { })
- mattkrea 8y agoI'm not sure what the problem is here. Your comment is a bit misleading since neither of those first two are expecting a callback. Both keys and values just return arrays so its not that out of place for entries to also return an array.
- db48x 8y agoObject.entries() doesn't take a function like that; it takes an object instance. It returns an array whose elements are themselves arrays, each one containing a key value pair. You could then call map on that array, which work the same way map always works. So why use an array of arrays like this? Most likely it's symmetry and composition. The inverse of this operation is to build an object from a list of key+value pairs, and the most natural way to represent that is with an array of two-element arrays. A similar pair of operations that uses an array of objects which carry more information is Object.getOwnPropertyDescriptors()/Object.create().