tc39 / proposal-built-in-modules

BSD 2-Clause "Simplified" License
892 stars 25 forks source link

Object map already 😒 #30

Closed StoneCypher closed 5 years ago

StoneCypher commented 5 years ago

who isn't sick of Object.keys(objThing).map( key => objThing[key] )

why isn't thing.map( (key, val) => ) a thing yet

tabatkins commented 5 years ago

Object.values() and Object.entries() already exist.

JS is generally loathe to add new things to Object.prototype, since it then shows up on every single object in the entire world (unless someone goes out of their way to null out the prototype).

It's then confusing when other types of objects define a different map operation, because you can't rely on the "default" map operation working everywhere. For example, Array.prototype.map takes a (value, index, collection)=>{...} callback, rather than the (key, val)=>{...} callback you list.

It probably would have been nice if "plain old objects" had a class of their own, a la Array, rather than being the root of the entire type hierarchy, but that's a ship long since sailed.

ljharb commented 5 years ago

Object.values, Object.entries, and Object.fromEntries (stage 3) all exist.

Adding a new method to Object.prototype is highly unlikely to ever be web compatible, and with Object.fromEntries(Object.entries(source).map(([key, value]) => […])) it remains to be seen if object mapping needs additional language support.

StoneCypher commented 5 years ago

nothing is needed

this would dramatically reduce complexity and unnecessary bugs

but okay

obedm503 commented 5 years ago

@StoneCypher you can always use lodash's map

bakkot commented 5 years ago

While @tabatkins and @ljharb are right to point out that we almost certainly couldn't add something to Object.prototype, we could provide a function with this behavior which took an object as an argument rather than a method which took the object as its receiver. I'm hesitant to do so, though: objects are pretty weird, what with accessors and non-enumerability and prototypes and proxies and so on. A ruby-style map makes sense for bare records, but less for JS's rich objects.

As such, I think there's advantage in making you say exactly what you mean. And with the addition of Object.fromEntries, that's gotten a lot easier. I'd like to let Object.fromEntries be out in the world for a while before deciding it's not sufficient.

StoneCypher commented 5 years ago

I mean mostly I just want something short

I end up writing byId and feMap in most of my code shrug