Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

133
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda