-2

How would access the _id and state values?

Here's the data

{
    "data": {
        "totalSamplesTested": "578841",
        "totalConfirmedCases": 61307,
        "totalActiveCases": 3627,
        "discharged": 56557,
        "death": 1123,
        "states": [
            {
                "state": "Lagos",
                "_id": "O3F8Nr2qg",
                "confirmedCases": 20555,
                "casesOnAdmission": 934,
                "discharged": 19414,
                "death": 207
            },
            {
                "state": "FCT",
                "_id": "QFlGp4md3y",
                "confirmedCases": 5910,
                "casesOnAdmission": 542,
                "discharged": 5289,
                "death": 79
            }
        ]
    }
}
Kunal Mukherjee
  • 5,775
  • 3
  • 25
  • 53
Tobi2612
  • 1
  • 1
  • 4
    Welcome to Stackoverflow. Please take the [tour](https://stackoverflow.com/tour) (and get a badge). Your question lacks any attempt at solving this problem on your own. The idea is for you to try to get something to work and then come here with specific problems you are unable to resolve. Taking the tour and reading about [How to ask a good question](https://stackoverflow.com/help/how-to-ask) in the help center will provide all the information you need. – Randy Casburn Oct 18 '20 at 16:58
  • Why do you think you need destructuring? – Yevhen Horbunkov Oct 18 '20 at 16:58
  • `data.states.map(({ _id }) => _id)` – Adam Azad Oct 18 '20 at 17:00
  • 2
    [How can I access and process nested objects, arrays or JSON?](https://stackoverflow.com/questions/11922383/how-can-i-access-and-process-nested-objects-arrays-or-json) – Guy Incognito Oct 18 '20 at 17:03
  • @AdamAzad I tried that but i received a type error of 'Cannot read property 'states' of undefined'. – Tobi2612 Oct 18 '20 at 17:10
  • Try with `response.data.states.map(({ _id, state }) => ({ _id, state }));` – Kunal Mukherjee Oct 18 '20 at 17:37

1 Answers1

0

What you have shown is a string in JSON format. You can convert that to a JavaScript object and then start to get the values you need from it.

let str = ‘{
    "data": {
        "totalSamplesTested": "578841",
        "totalConfirmedCases": 61307,
        "totalActiveCases": 3627,
        "discharged": 56557,
        "death": 1123,
        "states": [
            {
                "state": "Lagos",
                "_id": "O3F8Nr2qg",
                "confirmedCases": 20555,
                "casesOnAdmission": 934,
                "discharged": 19414,
                "death": 207
            },
            {
                "state": "FCT",
                "_id": "QFlGp4md3y",
                "confirmedCases": 5910,
                "casesOnAdmission": 542,
                "discharged": 5289,
                "death": 79
            }
        ]
    }
} ‘;

(Note, I have put the string in single quotes so it can be shown properly here but in your code you need to put it in back ticks so it can span many lines)

Now convert it to a JavaScript object.

let obj = JSON.parse(str);

Now look closely at the string to see how the object is structured. It actually has just one item in it, data. And that is itself an object with several items, one of which is states which is an array.

So, obj.data.states[0] is the array’s first entry. That is an object and has _id and state items.

You can step through the array extracting the ._id and .state entries.

A Haworth
  • 30,908
  • 4
  • 11
  • 14