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

132
Views
Proccessing nested object in an array of objects?

I have the below array of object from get request.

""

const submitHandler = async(e) =>{
    e.preventDefault()
    const groupForm = {
        leagueID,
        rank: parseInt(rank)
    }
    try {
        console.log(groupForm)
        const group = await axios.get(`/getGroup/${leagueID}/${parseInt(rank)}`)
        console.log(group)
        const group2 = group.data.map((obj,index)=>{
            const {team, ...rest} = obj;
            return({...rest, name: team.name})
        })
        console.log(group2)
        setGroupTable(group2)
    } catch (error) {
        console.log(error)
    }
}

""

Each object has a nested team object, from which I want to take the value of the team.name and add to parent object. Could someone help and advise me on how I can achieve this?

The reason, for doing this, is that I want array of object which will be retrived in a datatable. From my understand, data table accept an array of object for each row. Hence, I want to take the team.name value to the parent object.

++Update current vs desired array of object sample code

""

const group = [
  {
    '_id': "61f9b4b59f6601076fff7682",
    'league': "61f1db496de13b0505f08c83",
    'rank': 1,
    'team': {_id: '61f86e813bce5a7bfce4eb6a', name: 'ZizoTeam'},
    'createdAt': "2022-02-01T22:31:17.808Z",
    'updatedAt': "2022-02-01T22:31:17.808Z",
    '__v': 0
  }
]


const group2 = [
  {
    '_id': "61f9b4b59f6601076fff7682",
    'league': "61f1db496de13b0505f08c83",
    'rank': 1,
    "name": 'ZizoTeam',
    'createdAt': "2022-02-01T22:31:17.808Z",
    'updatedAt': "2022-02-01T22:31:17.808Z",
    '__v': 0
  }
]

""

Server query

""

    const teamsInGroup = await Group.find({league:req.params.leagueID, rank: req.params.rank}).populate('team', 'name')
    console.log(teamsInGroup)
    res.status(200).json(teamsInGroup)

""

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

0

const group2 = group.map(obj => {
  return {
    ...obj,
    team: obj.team.name,
  };
}
);
console.log(group2);

and to align with your output if you don't need team object and instead only want name

const group2 = group.map(obj => {
  const { team, ...rest } = obj;
  return { ...rest, name: team.name };
});
console.log(group2);
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!