My goal is to retrieve JSON data with a specific structure as shown below:
{"Points":
{"90":
{"0": {"name": "John Phillip", "slug": "john"},
{"1": {"name": "Mark Anthony", "slug": "mark"},
...
},
...
}
In my asynchronous function getData(93)
, I am fetching the data and processing it accordingly:
async getData(93) {
const res = await fetch("/json/sample.json");
const data = await res.json();
// round to the lowest multiple of five 5: 92->90, 93->90
const roundToLowest5 = x => Math.floor(x/5)*5
// Accessing the key '90' from the outer key 'Points'
console.log(data.Points[roundToLowest5(value).toString()])
return this.setState({ data });
}
The console currently displays all data for the outer key 'Points':
{0: {…}, 1: {…}, 2: {…}, 3: {…}, 4: {…}, 5: {…}, 6: {…}, 7: {…}, 8: {…}, 9: {…}, 10: {…}, 11: {…}}
Since all index keys ("0", "1", "2"...) are strings, how can I retrieve all key-value pairs for each index key?
Desired output:
{"name":"John Phillip", "slug": "john"}, {"name":"Mark Anthony", "slug":"mark"}...