DEV Community

Discussion on: JavaScript object destructuring usages you must know

Collapse
 
dagropp profile image
Daniel Gropp • Edited

Nice 😎 this is an amazing and very useful feature. Another useful thing with object destructing is to destruct some properties and then use the spread operator to get the rest.

const obj = {name: β€œMoses”, age: 120, country: β€œEgypt”};
const {name, …rest} = obj;
console.log(name); // β€œMoses”
console.log(rest); // {age: 120, country: β€œEgypt”}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
atapas profile image
Tapas Adhikary

Awesome Daniel, Thanks for adding to the list. Have a great day.

Collapse
 
maxiqboy profile image
Thinh Nguyen • Edited

Since it is ..., it's not sread operator.

In Object Destructuring, these three dots is rest propertises,

Rest in Object Destructuring
The Rest/Spread Properties for ECMAScript proposal (stage 4) adds the rest syntax to destructuring. Rest properties collect the remaining own enumerable property keys that are not already picked off by the destructuring pattern.

let {a, b, ...rest} = {a: 10, b: 20, c: 30, d: 40}
a; // 10
b; // 20
rest; // { c: 30, d: 40 }
Enter fullscreen mode Exit fullscreen mode