DEV Community

Cover image for JavaScript object destructuring tips

JavaScript object destructuring tips

Chris Bongers on July 24, 2022

Regarding JavaScript, we get an extremely useful way of extracting properties from objects. Note: Destructuring also works on arrays, but let's f...
Collapse
 
peerreynders profile image
peerreynders • Edited

… and destructuring can be used directly in the parameter list

function grade({ score }) {
  if (score >= 90) return 'A';
  if (score >= 80) return 'B';
  if (score >= 70) return 'C';
  if (score >= 60) return 'D';
  return 'F';
}

console.log(
  [
    { name: 'a', score: 91 },
    { name: 'b', score: 80 },
    { name: 'c', score: 79 },
    { name: 'd', score: 60 },
    { name: 'f', score: 59 },
  ].map(grade)
); // ["A", "B", "C", "D", "F"]
Enter fullscreen mode Exit fullscreen mode

Using the rest property to omit properties

const {b, c, ...redacted} = {a: 4, b: 'top', c: 'secret', d: 1};
console.log(redacted); // {a: 4, d: 1}
Enter fullscreen mode Exit fullscreen mode

Sadly Pattern Matching didn't make any progress at the 91st TC39 meeting.

How is this related?

"Pattern matching is a conditional construct and destructuring isn’t" [ref]

i.e. Pattern matching is destructuring on steroids.

const res = await fetch(jsonService)
match (res) {
  when ({ status: 200, headers: { 'Content-Length': s } }):
    console.log(`size is ${s}`);
  when ({ status: 404 }):
    console.log('JSON not found');
  when ({ status }) if (status >= 400): do {
    throw new RequestError(res);
  }
};
Enter fullscreen mode Exit fullscreen mode

Library-based example

(I always hate how switch is a statement and not an expression always forcing me to do the switch/case/return thing)

Collapse
 
artydev profile image
artydev

Great thank you

Collapse
 
bobgravity1 profile image
bobgravity1

this type of destructing is particularly useful for frameworks like Gatsby. It becomes a nightmare pulling data from GraphQL data layers. This stuff changed my skill level big-time. Glad someone covered it so in depth for those who don't know how to use it at this level

Collapse
 
dailydevtips1 profile image
Chris Bongers

Always happy to share πŸ’–

Collapse
 
elsyng profile image
Ellis • Edited

Destructure potentially empty objects
and avoid a js runtime error:

const { age } = user || {};
Enter fullscreen mode Exit fullscreen mode

(unless someone has a better idea.)

Collapse
 
sadid369 profile image
Sadid

game changing for me. Thanks a lot. Love From Bangladesh πŸ‡§πŸ‡©

Collapse
 
lisacee profile image
Lisa Cee

A really great summary.

Collapse
 
dailydevtips1 profile image
Chris Bongers

Thanks a lot Lisa πŸ’–

Collapse
 
murdej profile image
murdej

Thank, I'm a bit smarter againπŸ˜€

Collapse
 
dailydevtips1 profile image
Chris Bongers

Keep learning little bits every single day πŸ’–

Collapse
 
andrewbaisden profile image
Andrew Baisden

Good snippets thanks for putting them together.

Collapse
 
dailydevtips1 profile image
Chris Bongers

Glad you enjoyed them Andrew πŸ’–

Collapse
 
mjcoder profile image
Mohammad Javed

Another very good JS article. Keep them coming. πŸ‘πŸ’―

Collapse
 
dailydevtips1 profile image
Chris Bongers

Glad you are enjoying them πŸ’–

Collapse
 
dsmark profile image
Sanskar Sahu

Such a nice article you dude.

Collapse
 
dailydevtips1 profile image
Chris Bongers

Thanks a lot Sanskar πŸ’–

Collapse
 
artydev profile image
artydev

As usual, very useful, Thnaks Chris :-)

Collapse
 
dailydevtips1 profile image
Chris Bongers

Thanks a lot πŸ’–

Collapse
 
blckclov3r profile image
Aljun

Thank you

Collapse
 
dailydevtips1 profile image
Chris Bongers

Always happy to share πŸ’–

Collapse
 
bosspetta profile image
Enrique

Thanks a lot Chris, very useful β™₯️

Collapse
 
tykok profile image
Tykok

Really interesting, thank you for sharing this

Collapse
 
dailydevtips1 profile image
Chris Bongers

Always happy to share πŸ’–