The nested array, stats, consists of objects containing key-value pairs.
- Each key-value pair is an object with two elements: the first element is the property key and the second element is the property value.
- The goal is for statName and statValue to transformed to key:value pairs in the outermost array of objects.
I’ve identified two JavaScript functions on MDN Web Docs that seemed to be somewhat relevant: Object.formEntries() and Object.entries().
- The Object.fromEntries() static method transforms a list of key-value pairs into an object.
- The Object.entries() static method returns an array of a given object’s own enumerable string-keyed property key-value pairs
[
{
"playerName": "Chris Kirk",
"country": "United States",
"countryFlag": "USA",
"rank": 35,
"stats": [
{
"statName": "Avg",
"statValue": "70.356",
},
{
"statName": "Total Strokes",
"statValue": "5,176",
},
{
"statName": "Total Adjustment",
"statValue": "30.308",
},
{
"statName": "Total Rounds",
"statValue": "74",
}
]
},
{
"playerName": "Kevin Yu",
"country": "Chinese Taipei",
"countryFlag": "TPE",
"rank": 36,
"stats": [
{
"statName": "Avg",
"statValue": "70.386",
},
{
"statName": "Total Strokes",
"statValue": "3,251",
},
{
"statName": "Total Adjustment",
"statValue": "57.157",
},
{
"statName": "Total Rounds",
"statValue": "47",
}
]
}
]
What I’m trying to achieve.
[
{
"playerName": "Chris Kirk",
"country": "United States",
"countryFlag": "USA",
"rank": 35,
"Avg": "70.356",
"Total Strokes": "5,176",
"Total Adjustment":"30.308",
"Total Rounds": "74"
},
{
"playerName": "Kevin Yu",
"country": "Chinese Taipei",
"countryFlag": "TPE",
"rank": 36,
"Avg": "70.386",
"Total Strokes": "3,251",
"Total Adjustment": "57.157",
"Total Rounds": "47"
}
]
4
Answers
You can use map for modify old response and inside map you can loop for states values
What @evolutionxbox says. But here is a solution for your problem:
Running:
Here is a valid solution of your problem