如果我有一个对象数组
var bitcoinData = [
{
"date": "2013-05-01",
"txVolume(USD)": 108659660.293,
"txCount": 52443,
"marketcap(USD)": 1542820000,
"price(USD)": 139,
"exchangeVolume(USD)": 0,
"generatedCoins": 3575,
"fees": 36.80599998,
"activeAddresses": null
},
{
"date": "2013-05-02",
"txVolume(USD)": 96958519.0041,
"txCount": 55169,
"marketcap(USD)": 1292190000,
"price(USD)": 116.38,
"exchangeVolume(USD)": 0,
"generatedCoins": 3425,
"fees": 54.40791613,
"activeAddresses": null
如何使用map函数创建仅包含每天日期和价格的数组?
答案 0 :(得分:1)
命名“ price(USD)”会导致销毁问题。 所以用基本代替。给你。
const bitcoinData = [
{
"date": "2013-05-01",
"txVolume(USD)": 108659660.293,
"txCount": 52443,
"marketcap(USD)": 1542820000,
"price(USD)": 139,
"exchangeVolume(USD)": 0,
"generatedCoins": 3575,
"fees": 36.80599998,
"activeAddresses": null
},
{
"date": "2013-05-02",
"txVolume(USD)": 96958519.0041,
"txCount": 55169,
"marketcap(USD)": 1292190000,
"price(USD)": 116.38,
"exchangeVolume(USD)": 0,
"generatedCoins": 3425,
"fees": 54.40791613,
"activeAddresses": null
}];
const summary = bitcoinData.map(item => ({ date: item.date, price: item["price(USD)"] }));
console.log(summary);
解决了破坏性问题,这里使用的是这种技术。
const bitcoinData = [
{
"date": "2013-05-01",
"txVolume(USD)": 108659660.293,
"txCount": 52443,
"marketcap(USD)": 1542820000,
"price(USD)": 139,
"exchangeVolume(USD)": 0,
"generatedCoins": 3575,
"fees": 36.80599998,
"activeAddresses": null
},
{
"date": "2013-05-02",
"txVolume(USD)": 96958519.0041,
"txCount": 55169,
"marketcap(USD)": 1292190000,
"price(USD)": 116.38,
"exchangeVolume(USD)": 0,
"generatedCoins": 3425,
"fees": 54.40791613,
"activeAddresses": null
}];
const summary = bitcoinData.map(({ date, "price(USD)": price }) => ({ date, price }));
console.log(summary);
答案 1 :(得分:0)
完成
var bitcoinData = [
{
"date": "2013-05-01",
"txVolume(USD)": 108659660.293,
"txCount": 52443,
"marketcap(USD)": 1542820000,
"price(USD)": 139,
"exchangeVolume(USD)": 0,
"generatedCoins": 3575,
"fees": 36.80599998,
"activeAddresses": null
},
{
"date": "2013-05-02",
"txVolume(USD)": 96958519.0041,
"txCount": 55169,
"marketcap(USD)": 1292190000,
"price(USD)": 116.38,
"exchangeVolume(USD)": 0,
"generatedCoins": 3425,
"fees": 54.40791613,
"activeAddresses": null
}]
console.log(bitcoinData.map(data=> {
return {
date: data.date,
'price(USD)': data['price(USD)']
}})
)
答案 2 :(得分:0)
尝试一下:
bitcoinData.map(element => {
return ({"date":element.date, "price(USD)":element["price(USD)"]})
});
答案 3 :(得分:-2)
BitCoinData.map(bitCoin => {date: bitcoin.date, price: bitcoin.price});
这应该返回仅包含日期和价格的对象数组