Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

108
Views
Javascript destructuring and function as an object

Could someone possibly help me understand this function and destructuring a little better?

export default ({ names }) => {
  const { 
      people: { 
          children = [] 
        } = {} 
    } = names;
  children.find((child) => child.email);
};

I understand that names is being destructured to extract data stored in objects in order to find the first child in the children's array that has an email address. But what I don't understand is why is this function declared like this ({ names })? Is it because names is an object? Sorry if this is a silly question but I am struggling to understand this.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Lets break it down:

  • Your function takes in 1 parameter which is an object...
  • ...that object must have a property names.

Then the function destructures the object as follows (const { people: { children = [] } = {} } = names;):

  • First, you destructurize a property called people from the names argument
  • If people doesn't exist, it takes the default value of {}.
  • And finally, it grabs the property children (which are an array of objects) from its parent people.

Next, the function logic with .find()

  • All it does is searching for a child from children from people from names from the argument object that has a property email...
  • ...and returns it. Unfortunately that part is missing in your function code.

So in your snippet, the function actually does absolutely nothing, except to be unnecessarily complicated :P


To wrap things up. This is what your argument to the function could look like:

const argument = {
  names: {
    people: {
      children: [{ email: "myemail@mail.com", name: "thechildtobefound" }],
    }
  }
};

And here's a working sample to try:

const func = ({ names }) => {
  const { people: { children = [] } = {} } = names;
  return children.find((child) => child.email);
};

const argument = {
  names: {
    people: {
      children: [{ email: "myemail@mail.com", name: "thechildtobefound" }],
    },
  },
};

console.log(func(argument));

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!