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:
Thanks!
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})
);