JavaScript Destructure and assign to new object JavaScript Destructure and assign to new object typescript typescript

JavaScript Destructure and assign to new object


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);


You can use IIFE

const payload = { a: 1, b: 2, c: 3, d: 4, e: 5 }const obj = (({a,c,e}) => ({a,c,e}))(payload)console.log(obj)


You can create the object during destructuring using object rest:

const payload = { a: 1, b: 2, c: 3, d: 4, e: 5 }const { b, d, ...newPayload } = payloadconsole.log(newPayload)