Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does `Object.keys/values/entries` return Arrays while `Array...` return iterators

In ES6/ES2018+,

  • Object.keys(), Object.values(), Object.entries() seem to return concrete array types.
  • Array.keys(), Array.values(), Array.entries() seem to return iterators.

Is the reason for the disparity between these APIs?

like image 629
x70766c Avatar asked Jul 05 '26 09:07

x70766c


1 Answers

It's a matter of time. Object.keys was introduced with ES5, iterables were introduced with ES6. However, iterators do make more sense for all of them. Thus Object.values and Object.entries probably follow Object.keys for consistency, and the array methods use the new iterator interface.

like image 138
Jonas Wilms Avatar answered Jul 06 '26 23:07

Jonas Wilms