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

202
Vistas
How to extract data from nested objects array in React?

I have kind of a special problem. I am currently learning react and have fetched data from my own demo API (a Java Spring project).

However, I receive the data correctly in React. Now I am creating Components with it (Music Track Metadata). The response of the API returns an Array of Objects and one item within the object is a nested object. Now i don't know how i can extract this information to my React component.

Here is an example JSON of my response:

Object {
​​
duration: 200015
​​
explicit: false
​​
id: 8
​​
isrc: "AUDCB1701705"
​​
name: "Fireworks (feat. Moss Kena & The Knocks)"
​​
songInfo: Object { id: 7, fullTitle: "Fireworks by Purple Disco Machine (Ft. The Knocks & Moss Kena)", geniusUrl: "https://genius.com/Purple-disco-machine-fireworks-lyrics", … }
​​​
fullTitle: "Fireworks by Purple Disco Machine (Ft. The Knocks & Moss Kena)"
​​​
geniusUrl: "https://genius.com/Purple-disco-machine-fireworks-lyrics"
​​​
id: 7
​​​
trackMetadata: null

As you can see, songInfo is the nested object. My react component is like:

class ResultArea extends Component {

    constructor(props) {
        super(props);
        this.state = {
            tracks: []
        }
    }

    componentDidMount() {
        axios.get("http://localhost:8080/all")
            .then(response => {
                this.setState({
                    tracks: response.data
                })
            })
    }

    componentDidUpdate(prevProps, prevState, snapshot) {
        console.log("Component updated! State is now:");
        console.log(this.state.tracks);
    }

    render() {
        const tracklist = this.state.tracks;
        const tracks = tracklist.map(
            ({isrc, duration, explicit, fullTitle, geniusUrl}) =>
                <Track key={isrc} fullTitle={fullTitle} isrc={isrc} duration={duration} explicit={explicit} geniusUrl={geniusUrl} />
        );
        return (
            <React.Fragment>
                <div className="border">
                    <ul>
                        {tracks}
                    </ul>
                </div>
            </React.Fragment>
        )
    }
}

The "state" of my component holds the result of the API request. How does this work? I've read some posts about nested objects, but my approach is to render a "<Track ... /> item for each track, so the suggested methods of the posts do not work for me.... I need the "geniusUrl" and the "fullTitle" value from the nested songInfo-Object...

Many thanks!

EDIT: Removed a comment.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

So, your response.data is an array of 'tracks'. Each 'track' is an Object. You are currently destructuring your 'track' like so, in your .map() method:

{isrc, duration, explicit, fullTitle, geniusUrl}

But.... fullTitle and geniusUrl are not direct keys of your 'track' object, but rather it's songInfo key, so you'd have to adjust your destructuring:

{isrc, duration, explicit, songInfo: {fullTitle, geniusUrl}}
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