I am in need of restructuring the data retrieved from an API call, which currently looks like this:
{
"Label3":
[
{ "name": "superman", "power": 8900 },
{ "name": "iron man", "power": 3000 },
{ "name": "spike spiegal", "power": 200 },
{ "name": "doctor strange", "power": 800 }
],
"Label18":
[
{ "name": "goku", "power": 9000 },
{ "name": "vegeta", "power": 8500 },
{ "name": "pringles can", "power": 1 }
],
"Label37":
[
{ "name": "dr manhattan", "power": 100000 },
{ "name": "mayonaise", "power": 10 }
]
}
My goal is to extract the highest power value from each array and reorganize the structure to look like this:
{
"groupName": 'Label3',
"highestPower": 8900,
"heroes": [
{"name":"superman","power":8900},
{"name":"iron man","power":3000},
{"name":"spike spiegal","power":200},
{"name":"doctor strange","power":800}
],
},
{
"groupName": 'Label18',
"highestPower": 9000,
"heroes": [
{"name":"goku","power":9000},
{"name":"vegeta","power":8500},
{"name":"pringles can","power":1}
],
},
{
"groupName": 'Label37',
"highestPower": 100000,
"heroes": [
{"name":"dr manhattan","power":100000},
{"name":"mayonaise","power":10}
],
}
I have been considering using a forEach loop on each array element to find the maximum value and assign it to a variable for comparison. However, I am unsure how to proceed with restructuring the data as required.