简体   繁体   English

如何将对象转换为数组并保存对象中的 id

[英]How to convert objects to array and save the id's from the objects

i have some problem that i'm tried to figure out for a while but nothing help me cus i have some issue with create div with information that came from API.我有一些问题,我试图解决一段时间,但没有任何帮助,因为我在使用来自 API 的信息创建 div 时遇到了一些问题。

let me explain what issue exactly i have and then what problem让我解释一下我到底有什么问题,然后是什么问题

firs: i get API from cmc this is two url冷杉:我从 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()

*first came as array from api *首先来自 api 的数组*首先来自api的数组

*second came as object from api *第二个来自 api 的 object *第二个来自api的对象

the first one it's for all data, the second one it's for the logo image of each coin cuz the firs url does not have logo so i need to take the url logo from the second to first.第一个用于所有数据,第二个用于每个硬币的徽标图像,因为第一个 url 没有徽标,所以我需要将 url 徽标从第二个到第一个。 everything works fine but when i try to make Search Result in my website to find some coin with filter&includes actually its showed the most information fine except the logo, the logo always stay on the same position but the rest data looks fine.一切正常,但是当我尝试在我的网站上搜索结果以查找带有过滤器和包含的硬币时,实际上它显示的信息最多,除了徽标,徽标始终保持在相同的 position 但 rest 数据看起来很好。

-the problem may to be that's not possible to do filter on object... if you guys have solution to my question pleas. - 问题可能是不可能在 object 上进行过滤......如果你们有解决我的问题的方法。

basically i need to get the second array exactly like the first one, but the id of numbers will stay outside and in the same order and then its solve my problem like this:基本上我需要得到第二个数组,就像第一个数组一样,但是数字的 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",
}]

but it's very important to get the id's out side of the new array and no inside of但是将 id 放在新数组的外面而不是里面是非常重要的

That is not valid syntax.那不是有效的语法。

const toThis = [
1: {
    id: 825,
    name: "Tether",
    symbol: "USDT",
},
825: {
    id: 825,
    name: "Tether",
    symbol: "USDT",
}]

Uncaught SyntaxError: Unexpected token ':'

You can put the objects in an array ie您可以将对象放入数组中,即

const toThis = [
{
    id: 825,
    name: "Tether",
    symbol: "USDT",
},
{
    id: 825,
    name: "Tether",
    symbol: "USDT",
}]

Your toThis variable doesn't make sense, you can have an array of objects but, you wouldn't be able to have the id as you've described above unless you changed the structure of the variable.您的toThis变量没有意义,您可以拥有一个对象数组,但是除非您更改变量的结构,否则您将无法拥有如上所述的id

You could potentially achieve that with a Map() however with something like this:您可以使用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" }
 }

Or alternatively just creating an array of objects (but you won't be able to get the id outside of the object as you've specified) by doing something like或者只是创建一个对象数组(但您将无法按照您的指定在 object 之外获取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" }
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM