[proposal] Object.pick

Isiah Meadows isiahmeadows at gmail.com
Wed Jul 25 23:41:51 UTC 2018


Just as a heads up, this has come up plenty of times before, both as a
function and (usually) syntax:
https://www.google.com/search?q=site%3Aesdiscuss.org+pick

I'm good with it being a built-in function, but it's harder to justify
using syntax apart from an easier time building up the relevant type
ICs.
-----

Isiah Meadows
me at isiahmeadows.com
www.isiahmeadows.com


On Wed, Jul 25, 2018 at 5:45 PM, Mikkel Davis <mikkeld at gmail.com> wrote:
> I've seen proposals for syntax additions that give functionality similar to
> lodash's "pick" utility. But I think it may be more appropriate to add it as
> an Object utility in the same vein of `Object.assign`, `Object.keys`,
> `Object.values`. After a quick search, I was surprised to not find an
> existing proposal like this. Why would that be?
>
> For me, this isn't just one of those "huh, that's a neat lodash util that
> would be nifty to have natively." The codebase I work on most often already
> has lodash everywhere, and I have found myself utilizing _.pick frequently,
> e.g. mapping or passing down portions of React state or props. I really
> think is common enough that it justifies addition to the spec.
>
> ```js
> const person = {
>     first: 'Marie',
>     last: 'Curie',
>     age: 51,
>     address: 'Grenoble, France',
> };
>
> Object.pick(person, ['first', 'age']);  // {first: "Marie", age: 51}
> ```
>
> The only other concise way to do this I can think of, using latest ES is
> `(({first, age}) => ({first, age}))(person)`, which is quite undesirable,
> for a variety of reasons.
>
> Another name that might be nice is `Object.slice`.
>
> ---
> Mikkel R. Davis
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>


More information about the es-discuss mailing list