gpt4 book ai didi

javascript - 如何将 JSON 响应的特定元素插入数组?

转载 作者:行者123 更新时间:2023-12-02 23:52:20 25 4
gpt4 key购买 nike

我正在使用 Nomics 加密货币 API。这是 Axios 调用:

axios.get(apiURL + apiKey + apiSpecs)
.then(function (response) {
// sort data by highest market cap
console.log(response.data)
})

以及相应的 JSON 响应:

[{ circulating_supply: '70126880',
high: '7.18687000',
high_timestamp: '2018-01-05T00:00:00.000Z',
market_cap: '58767727.57',
max_supply: '100000000',
price: '0.83802000',
price_date: '2019-04-08',
currency: 'ETP',
rank: '96' },
{ circulating_supply: '350000000',
high: '2.83125000',
high_timestamp: '2018-01-10T00:00:00.000Z',
market_cap: '54558000.00',
max_supply: null,
price: '0.15588000',
price_date: '2019-04-08',
currency: 'QASH',
rank: '97' },
... ]

如何返回仅包含货币属性的数组,例如 var arr = [ETP, QASH, ...]

最佳答案

使用 map :

const response = [{
circulating_supply: '70126880',
high: '7.18687000',
high_timestamp: '2018-01-05T00:00:00.000Z',
market_cap: '58767727.57',
max_supply: '100000000',
price: '0.83802000',
price_date: '2019-04-08',
currency: 'ETP',
rank: '96'
},
{
circulating_supply: '350000000',
high: '2.83125000',
high_timestamp: '2018-01-10T00:00:00.000Z',
market_cap: '54558000.00',
max_supply: null,
price: '0.15588000',
price_date: '2019-04-08',
currency: 'QASH',
rank: '97'
}
];

const currencies = response.map(({ currency }) => currency);
console.log(currencies);

ES5 语法:

var response = [{
circulating_supply: '70126880',
high: '7.18687000',
high_timestamp: '2018-01-05T00:00:00.000Z',
market_cap: '58767727.57',
max_supply: '100000000',
price: '0.83802000',
price_date: '2019-04-08',
currency: 'ETP',
rank: '96'
},
{
circulating_supply: '350000000',
high: '2.83125000',
high_timestamp: '2018-01-10T00:00:00.000Z',
market_cap: '54558000.00',
max_supply: null,
price: '0.15588000',
price_date: '2019-04-08',
currency: 'QASH',
rank: '97'
}
];

var currencies = response.map(function(item) {
return item.currency;
});
console.log(currencies);

关于javascript - 如何将 JSON 响应的特定元素插入数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55583404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com