
ASK: Did Ecmascript 2017 screw up Object.entries()? - thinkloop
&#x2F;&#x2F; keys<p>Object.keys(key =&gt; {})<p>&#x2F;&#x2F; values<p>Object.values(value =&gt; {})<p>&#x2F;&#x2F; * * entries * *<p>Object.entries(weirdArray =&gt; { 
  key = weirdArray[0]; 
  value = weirdArray[1];
})<p>Why not:<p>Object.entries(entry =&gt; {
  key = entry.key; 
  value = entry.value;
})<p>Or:<p>Object.entries((key, value) =&gt; {})
======
db48x
Object.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().

------
wakeywakeywakey
You can use array destructing, when supported:

Object.entries(([k, v]) => { })

~~~
wakeywakeywakey
Typed that on my phone, should be:

Object.entries({}).forEach(([k, v]) => { })

------
mattkrea
I'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.

