Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Destructure and assign to new object

In JavaScript/Typescript,

What is the short version to destructure and then assign in a new object like so :

const payload: MyPayload = { a: 1, b: 2, c: 3, d: 4, e: 5 }

// Destruct
const { a, c, e } = payload;

// New Obj
const newPayload = {
  a, c, e
};
like image 365
Scaraux Avatar asked Mar 30 '19 18:03

Scaraux


Video Answer


1 Answers

You could take a destructuring assignment with the object and short hand properties for a new object.

const
    getParts = ({ a, c, e }) => ({ a, c, e }),
    payload = { a: 1, b: 2, c: 3, d: 4, e: 5 },
    parts = getParts(payload);

console.log(parts);
like image 112
Nina Scholz Avatar answered Sep 20 '22 17:09

Nina Scholz