Reputation: 877
I have an object like this:
let a = { x: 3, y: '3', z: 'z' };
And an array like this:
let b = [{ x: 1, y: '1'}, { x: 2, y: '2' }];
How can I do something like this:
b.push({ x, y } = a);
Instead of this:
b.push({ x: a.x, y: a.y });
// or this:
const { x, y } = a;
b.push({ x, y });
Upvotes: 1
Views: 903
Reputation: 386604
You need to return a new object with the destructured properties.
const
getXY = ({x, y}) => ({ x, y }),
a = { x: 3, y: '3', z: 'z' },
b = [{ x: 1, y: '1'}, { x: 2, y: '2' }];
b.push(getXY(a));
console.log(b);
Upvotes: 1