Skip to content
Advertisement

How to map through array of objects to extract objects value?

i am trying to map through an array of objects to extract some value from each object.

When i run my code i am getting a result of undefined on console.log(open) but get all the data get log for the console.log(data)

Can someone tell me why is it undefined or what is wrong in my code?

thanks in advance

//Data from CCompare

CryptoCompareAPI.histoHour('BTC', 'USD')
.then(data => { 
  data = data.reverse()
  console.log(data)
  const open = data.map( d =>  d[3])
 
  console.log(open)

 
  tulind.indicators.sma.indicator([close],[3],(error,res) => {
     if (error) return log(error)
     console.log(res)
     })

  
  }
  //console.log("high",data[i].high)
  //console.log(data.length)
 
})
.catch(console.error())

Data

[{
    time: 1638356400,
    high: 57274.38,
    low: 56837.03,
    open: 57014.22,
    volumefrom: 761.03,
    volumeto: 43377564.28,
    close: 57193.03,
    conversionType: 'direct',
    conversionSymbol: ''
  },
  {
    time: 1638352800,
    high: 57212.91,
    low: 56897.87,
    open: 57144.92,
    volumefrom: 884.14,
    volumeto: 50454456.17,
    close: 57014.22,
    conversionType: 'direct',
    conversionSymbol: ''
  },
  {
    time: 1638349200,
    high: 57197.5,
    low: 56769.43,
    open: 56935.98,
    volumefrom: 1157.5,
    volumeto: 65981513.98,
    close: 57144.92,
    conversionType: 'direct',
    conversionSymbol: ''
  },
  {
    time: 1638345600,
    high: 57851.44,
    low: 56863.93,
    open: 57169.38,
    volumefrom: 2238.66,
    volumeto: 128240428.38,
    close: 56935.98,
    conversionType: 'direct',
    conversionSymbol: ''
  },
  ... 90 more items
]

Advertisement

Answer

const data = [{
    time: 1638356400,
    high: 57274.38,
    low: 56837.03,
    open: 57014.22,
    volumefrom: 761.03,
    volumeto: 43377564.28,
    close: 57193.03,
    conversionType: 'direct',
    conversionSymbol: ''
  },
  {
    time: 1638352800,
    high: 57212.91,
    low: 56897.87,
    open: 57144.92,
    volumefrom: 884.14,
    volumeto: 50454456.17,
    close: 57014.22,
    conversionType: 'direct',
    conversionSymbol: ''
  },
  {
    time: 1638349200,
    high: 57197.5,
    low: 56769.43,
    open: 56935.98,
    volumefrom: 1157.5,
    volumeto: 65981513.98,
    close: 57144.92,
    conversionType: 'direct',
    conversionSymbol: ''
  },
  {
    time: 1638345600,
    high: 57851.44,
    low: 56863.93,
    open: 57169.38,
    volumefrom: 2238.66,
    volumeto: 128240428.38,
    close: 56935.98,
    conversionType: 'direct',
    conversionSymbol: ''
  },
]

const open = data.map(e => e.open)
console.log(open)
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement