0

I am working with the Nomics cryptocurrency API. Here is the Axios call:

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

And corresponding JSON 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' }, 
... ]

How can I return an array of just the currency attributes such as var arr = [ETP, QASH, ...]

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
Matt
  • 78
  • 7
  • Possible duplicate of [From an array of objects, extract value of a property as array](https://stackoverflow.com/questions/19590865/from-an-array-of-objects-extract-value-of-a-property-as-array) – Tyler Roper Apr 09 '19 at 00:12
  • 1
    `const myArrayData = response.data.map(data => data.currency);` – Jonathan Rys Apr 09 '19 at 00:13
  • Welcome to Stack Overflow! Please take the [tour](https://stackoverflow.com/tour), visit the [help center](https://stackoverflow.com/help), and read up on [asking good questions](https://stackoverflow.com/help/asking). After doing some research and [searching](https://stackoverflow.com/help/searching) for related topics on SO, try it yourself. If you're stuck, post a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) of your attempt note exactly where you're stuck. People will be glad to help. – Scott Sauyet Apr 09 '19 at 00:16

1 Answers1

1

Use 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 syntax:

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);
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79