Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

139
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda