'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)

""



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source