Skip to content
Advertisement

Get the id of an object in a json request? [closed]

So I have to get the first object of this list:

{
"2489": {
    "status": {
        "idstatus": "3",
        "status": "Sold"
    }
},

"2490": {
    "status": {
        "idstatus": "3",
        "status": "Sold"
    }
}

}

I don’t know beforehand the IDs [‘2489’] and [‘2490’]. Sometimes it responses only one of these IDs, sometimes 3 or more.

How can I get like [‘2490’].status.idstatus without knowing [‘2490’]?

Advertisement

Answer

you can do it using Object.values and map or if you need the key you can use Object.entries

const data = {
"2489": {
    "status": {
        "idstatus": "3",
        "status": "Sold"
    }
},

"2490": {
    "status": {
        "idstatus": "3",
        "status": "Sold"
    }
}
}

const statusIds = Object.values(data).map(d => d.status.idstatus)
console.log(statusIds)

const statusIdAndKey = Object.entries(data).map(([k, d]) => [k, d.status.idstatus])
console.log(statusIdAndKey)
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement