3

If I have an array of objects

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

How can I create an array containing only the date and price of each day by using the map function?

0

4 Answers 4

5

The naming "price(USD)" causes a problem for destructuring. So used basic instead. Here you go.

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);

Solved the destructuring issue, here it is using that technique.

    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);

Sign up to request clarification or add additional context in comments.

Comments

0

Done

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)']
 }})
 )

Comments

0

Try this:

bitcoinData.map(element => {
    return ({"date":element.date, "price(USD)":element["price(USD)"]})
});

Comments

-2
BitCoinData.map(bitCoin => {date: bitcoin.date, price: bitcoin.price});

This should return an array of objects containing only the date and price

1 Comment

This code won't even run. You should wrap the curly braces with some parenthesis.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.