I have this multidimensional object from an API:
let curr = {
"base_currency_code": "EUR",
"base_currency_name": "Euro",
"amount": "10.0000",
"updated_date": "2024-04-14",
"rates": {
"USD": {
"currency_name": "United States dollar",
"rate": "1.0649",
"rate_for_amount": "10.6489"
},
"AUD": {
"currency_name": "Australian dollar",
"rate": "1.6444",
"rate_for_amount": "16.4443"
},
"CAD": {
"currency_name": "Canadian dollar",
"rate": "1.4669",
"rate_for_amount": "14.6690"
}
},
"status": "success"
}
And I want a sorted array of objects like this :
let curr = [
{
"id": "AUD",
"currency_name": "Australian dollar",
"rate": "1.6444",
"rate_for_amount": "16.4443"
},
{
"id": "CAD",
"currency_name": "Canadian dollar",
"rate": "1.4669",
"rate_for_amount": "14.6690"
},
{
"id": "USD",
"currency_name": "United States dollar",
"rate": "1.0649",
"rate_for_amount": "10.6489"
}
]
The goal would be to adapt it for a Flatlist in react-native.
Been trying many tricks but nothing works. Any suggestions?
2
Answers
On a first step you can map the array to the array you need
And then simply sort by id
Using
Object.keys(curr.rates)
you get the IDs array and a followingmap
andsort
on it can achieve the desired result.