I use nomics crypto APIs for data. My URL for the API call is -'http://api.nomics.com/v1/currencies/ticker?key=my-key&ids=BTC,ETH,BNB,DOGE,ADA,XRP,USDT,DOT,BCH,LTC&interval=1d'
So using this URL I want to call the coins data in the same order as they are written in the URL. But when I call it, the JSON file is sorted automatically by highest market_cap.
My backend code is-
export async function getCryptoBitcoinInfo() {
const url = 'https://api.nomics.com/v1/currencies/ticker?key=';
const key = await getSecret("Nomics_api_key");
let fullUrl = url + key + '&ids=BTC,ETH,BNB,DOGE,ADA,XRP,USDT,DOT,BCH,LTC&interval=1d'
console.log("Url: " + fullUrl);
return fetch(fullUrl, {method: 'get'})
.then(response => response.json())
}
And my front end code is -
$w.onReady(function(){
getCryptoBitcoinInfo()
.then(currencyInfo => {
$w('#text502').text = currencyInfo[3].price
})
})
The above code should pull data for DOGE data but pulls XRP data.
I am using the Wix javascript console ie Velo for coding. To maintain the earlier order, I need to sort the JSON file. How can I do it without affecting my URL endpoint?? What's the code required in js for this?? Any help would be really appreciated.
Below is the JSON data format
[
{
"id": "BTC",
"currency": "BTC",
"symbol": "BTC",
"name": "Bitcoin",
"logo_url": "https://s3.us-east-2.amazonaws.com/nomics-api/static/images/currencies/btc.svg",
"status": "active",
"price": "49893.44419788",
"price_date": "2021-05-13T00:00:00Z",
"price_timestamp": "2021-05-13T15:52:00Z",
"circulating_supply": "18709093",
"max_supply": "21000000",
"market_cap": "933461087588",
"market_cap_dominance": "0.3950",
"num_exchanges": "384",
"num_pairs": "60196",
"num_pairs_unmapped": "5078",
"first_candle": "2011-08-18T00:00:00Z",
"first_trade": "2011-08-18T00:00:00Z",
"first_order_book": "2017-01-06T00:00:00Z",
"rank": "1",
"rank_delta": "0",
"high": "63511.58620446",
"high_timestamp": "2021-04-13T00:00:00Z",
"1d": {
"volume": "122857986900.35",
"price_change": "-5592.21908418",
"price_change_pct": "-0.1008",
"volume_change": "43071195664.36",
"volume_change_pct": "0.5398",
"market_cap_change": "-104568141203.38",
"market_cap_change_pct": "-0.1007"
}
},
{ "id": "ETH"
...
},
{ "id": "BNB"
...
},
{ "id": "ADA"
...
},
{ "id": "XRP"
...
},
...
]
Sorting is one way to do it but a really precise way to find your data would be to use the findIndex function.
For example:
const dogeIndex = currencyInfo.findIndex(currencyData => currencyData.id === "DOGE")
So then your new frontend code would be something like:
$w.onReady(function(){
getCryptoBitcoinInfo()
.then(currencyInfo => {
const dogeIndex = currencyInfo.findIndex(currencyData => currencyData.id === "DOGE")
$w('#text502').text = currencyInfo[dogeIndex].price
})
})
If you still really want to use sorting then you can sort the data on the frontend by calling:
currencyInfo.sort((a,b)=> a.id > b.id ? 1 : -1)
This is one way to sort the data json.
Basically, you need to have a JS map which contains the order in which each currency should appear.
const order = { 'BTC': 1, 'ETH': 2, 'BNB': 3, 'DOGE': 4 };
Then you use the Array.sort
to sort them based on the index from previous object.
sampleData.sort((data1, data2) => order[data1.id] - order[data2.id]);
Check and run the below snippet for better understanding.
const order = { 'BTC': 1, 'ETH': 2, 'BNB': 3, 'DOGE': 4, 'ADA': 5, 'XRP': 6, 'USDT': 7, 'DOT': 8, 'BCH': 9, 'LTC': 10 }; const sampleData = [ { id: 'BTC' }, { id: 'DOGE' }, { id: 'ETH' }, ]; const sortedData = sampleData.sort((data1, data2) => order[data1.id] - order[data2.id]); console.log(sortedData);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.