Destructured objects question


Write a function called numberGroups that will take a destructured object as it’s parameter.

The object properties will be named a, b, and c and their values will be arrays of numbers.

Find the longest array and return that array.


I’m in a coding bootcamp and they aren’t explaining anything. They touch on concepts and expect us to go and google things and figure it out.

I’m not even sure what to google with this. I’ve looked at multiple resources for destructured objects and I’m still not figuring it out.

Destructing is a way to “take apart” an object or an array.

For example:

const obj = { x: 12 };
const { x } = obj;
// 12

Note the destructuring on the second line. This is the equivalent of const x = obj.x;

It’s just a “cleaner” way to do it.

But your problem is in a function. Let’s take this example:

const sumProperties = theDataObject => {
  const { x, y } = theDataObject;
  return x + y;

const obj = { x: 12, y: 23 };
// 35

We pass in an object and destructure it. But they want you to “take a destructured object as it’s parameter.” I think you want to destructure in the parameter list:

const sumProperties = ({ x, y })  => {
  return x + y;

This does the exact same thing as the previous one, it just skips the middle man and doesn’t store the object in a variable but destructures it right in the parameter list. Of course, using an implicit return we could shorten this to:

const sumProperties = ({ x, y })  => x + y;

Slick, huh?

Yeah, it can be frustrating to learn this stuff. And part of being a good developer is being able to find answers - no one can memorize it all. MDN is a great site - here’s their page. And I think if you google “javascript object destructuring” you will find some good pages and videos.