Accessing (n)th key from an object

T.J. Crowder tj.crowder at
Tue Apr 24 15:42:15 UTC 2018

On Tue, Apr 24, 2018 at 4:24 PM, Michael Luder-Rosefield
<rosyatrandom at> wrote:
> I've found myself using that pattern, actually. Say you have multiple
> variables and want to pass one of them on to a function, which wants to know
> the value (of course) of that variable but also some kind of name to know
> which one was passed on.
> The easiest way to do this in one pass at the stage you pass it on is for
> the variable name to match the one needed later, and send it to the function
> as the object `{ whateverName }`.

On those rare occasions, the way I'd use would be `["whateverName",
whateverName]` (granted it's more verbose at the call site). That's
what `Object.entries` and `Map.prototype.entries` do, for instance,
and it's friendly to destructuring:

function example([name, value]) {
    console.log(`${name} = ${value}`);
example(["answer", 42]);

-- T.J. Crowder

More information about the es-discuss mailing list