1

I like the style of array method chaining with map, filter, etc, but often find myself having to "break" that style in order to use Object.keys(), Object.values(), Object.entries() and Object.fromEntries(). I discovered the Object instance method getOwnPropertyNames which seems comparable to Object.keys, if less elegantly named, but I'm wondering about instance versions of the other static Object methods.

For example, instead of writing:

const foo = Object.fromEntries(Object.keys(bar).map(myCoolFunction))

I'd like to be able to write something like:

const foo = bar.keys()
  .map(myCoolFunction)
  .toObjectFromEntries()

Peter Alfvin
  • 28,599
  • 8
  • 68
  • 106
  • The Object methods were added fairly recently, and added as "static" methods to avoid breaking any of the massive amount of JavaScript code in the wild. – Pointy Nov 23 '21 at 14:37
  • 1
    One of the main reasons, why i am waiting for https://github.com/tc39/proposal-pipeline-operator – ASDFGerte Nov 23 '21 at 14:39
  • @ASDFGerte also relevant is the [bind syntax proposal](https://github.com/tc39/proposal-bind-operator) which allows free methods to easily be executed against an instance. In essence replacing `someMethod.call(someInstance, arg1, arg2)` with `someInstance::someMethod(arg1, arg2)`. Unfortunately, there doesn't seem to be much movement on it :/ – VLAZ Nov 23 '21 at 14:41
  • Wow. Thanks for that pipeline operator reference, @ASDFGerte. Didn't realize how well trodden this ground was. – Peter Alfvin Nov 23 '21 at 14:46
  • Sadly, the first commit to that proposal's repo was "Nov 9, 2015". I understand it's complex, and making a wrong decision means having to deal with the consequences for decades (can't undo), but i'd still wish it would finally get somewhere usable. – ASDFGerte Nov 23 '21 at 14:52
  • @ASDFGerte Yeah. The more function-y proposals have been dragging on, unfortunately. – VLAZ Nov 23 '21 at 14:54

0 Answers0