我有一些問題,我試圖找出一段時間,但沒有任何幫助,因為我在使用來自 API 的資訊創建 div 時遇到了一些問題。
讓我解釋一下我到底有什么問題,然后是什么問題
冷杉:我從 cmc 獲得 API,這是兩個 url
const bitcoinData = await fetch(`https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?start=${num}&limit=${numberOfCoins}&CMC_PRO_API_KEY=${apiKey.key}`)
const bitcoinD = await bitcoinData.json()
const bitcoinInfo = await fetch(`https://pro-api.coinmarketcap.com/v1/cryptocurrency/info?id=${array}&CMC_PRO_API_KEY=${apiKey.key}`)
const bitcoinI = await bitcoinInfo.json()
*首先是來自 api 的陣列

*第二個來自api的物件

第一個用于所有資料,第二個用于每個硬幣的徽標影像,因為第一個 url 沒有徽標,因此我需要將 url 徽標從第二個移到第一個。一切正常,但是當我嘗試在我的網站上創建搜索結果以找到一些帶有過濾器和包含的硬幣時,實際上它顯示的資訊最多,除了徽標外,徽標始終保持在同一位置,但其余資料看起來不錯。
- 問題可能是無法對物件進行過濾......如果你們有解決我的問題的方法。
基本上我需要像第一個陣列一樣獲得第二個陣列,但是數字的 id 將保持在相同的順序之外,然后它可以像這樣解決我的問題:
fromThis = {
1: {
id: 825,
name: "Tether",
symbol: "USDT",
},
825: {
id: 825,
name: "Tether",
symbol: "USDT",
},
toThis = [
1: {
id: 825,
name: "Tether",
symbol: "USDT",
},
825: {
id: 825,
name: "Tether",
symbol: "USDT",
}]
但是將 id 放在新陣列的外面而不是里面是非常重要的
uj5u.com熱心網友回復:
那不是有效的語法。
const toThis = [
1: {
id: 825,
name: "Tether",
symbol: "USDT",
},
825: {
id: 825,
name: "Tether",
symbol: "USDT",
}]
Uncaught SyntaxError: Unexpected token ':'
你可以把物件放在一個陣列中,即
const toThis = [
{
id: 825,
name: "Tether",
symbol: "USDT",
},
{
id: 825,
name: "Tether",
symbol: "USDT",
}]
uj5u.com熱心網友回復:
您的toThis變數沒有意義,您可以擁有一個物件陣列,但是id除非您更改了變數的結構,否則您將無法擁有上述的物件。
您可以使用Map()實作這一點,但是使用以下內容:
const toThis = new Map();
for (const id in fromThis) {
toThis.set(id, fromThis[id]);
}
// toThis map output
Map {
1: { id: 1, name: "Tether", symbol: "USDT" },
825: { id: 825, name: "Tether", symbol: "USDT" }
}
或者只是id通過執行類似的操作來創建一個物件陣列(但您將無法按照指定的方式獲取物件的外部)
const toThis = [];
for (const id in fromThis) {
toThis.push(fromThis[id]);
}
// toThis array output
[
{ id: 1, name: "Tether", symbol: "USDT" },
{ id: 825, name: "Tether", symbol: "USDT" }
]
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/364990.html
標籤:javascript 数组 搜索 筛选 转换器
下一篇:選擇并加入sequelize
