-1

I would like to use curly braces to replace this code:

let rUsers = children.map((user) => {
    let rUser= {};
    rUser.firstName = user.firstName;
    rUser.lastName = user.lastName;
    rUser.relation = user.patron.relation;
    return rUser;
 });

This is what I have:

let rUsers = children.map((user) => {
    let rUser = {};
    ({
        firstName: rUser.firstName,
        lastName: rUser.lastName,
        patron.relation: rUser.relation, // I get an error here
    } = user);
    return rUser;
}

Except for patron.relation, the rest of the extraction works.

Questions:

  1. How should I extract value of user.patron.relation?
  2. Is there a more succint way of writing the above?

Thanks!

fab
  • 382
  • 2
  • 19

3 Answers3

1

You can use object destructuring like this:

const {firstName, lastName, patron: {relation}} = user

And relation has been set to the value of user.patron.relation.

You can return the object you want like so:

return {firstName, lastName, relation}
gunn
  • 7,959
  • 2
  • 22
  • 23
1

How should I extract value of user.patron.relation?

You would use { … patron: {relation: rUser.relation}, … } as the destructuring target.

Is there a more succint way of writing the above?

If the property names don't match there's not much you can do, but in your particular case you can simplify by destructuring the parameter into multiple variables:

const rUsers = children.map(({firstName, lastName, patron: {relation}}) => 
    ({firstName, lastName, relation})
);
Bergi
  • 513,640
  • 108
  • 821
  • 1,164
  • Thanks @Bergi for answering both ! Worked well! In cases where properties can't be passed as parameters, how would you extract specific properties from one object and assign them to another object? – fab Nov 16 '17 at 23:26
  • @fab What do you mean by "properties that can't be passed as parameters"? – Bergi Nov 17 '17 at 00:15
  • in the above, we were able to access array's map() method, and pass the object properties as parameters. How about if there's an object user: `let user = { firstname: 'Aldrin', lastname: 'Theo', patron: { relation: 'Spouse' } }` and we want to extract `firstname` and `patron.relation` into an object `reducedUser`. How would you destructure that? – fab Nov 17 '17 at 03:52
  • @fab Either just destructure into a `let`/`const` and then build the object, or [do all in one IIFE expression](https://stackoverflow.com/q/25553910/1048572) (with parameters :-P) – Bergi Nov 17 '17 at 11:18
  • Okay. Thanks @Bergi! – fab Nov 18 '17 at 07:43
0

I'm seeing a few things wrong with your code.

  1. patron.relation -> patron isn't defined, and can't be used as a key.
  2. In the second example, rUser is used - I think you just want user.

My understanding of what you're trying to do essentially create a clone? If so, you should be able to do something like this.

let rUsers = children.map(user => {
    firstName: user.firstName,
    lastName: user.lastName,
    relation: user.relation
});

Alternatively, it could be simpler to use Object.assign()

let rUsers = children.map(user => Object.assign({}, user));
jhpratt
  • 5,613
  • 16
  • 32
  • 43