0

I have an api call that produces the follow results in the console (after pairing it down using map() ).

{…}
​
CHANGE24HOUR: "$ 11.16"
​
CHANGEDAY: "$ 3.31"
​
CHANGEPCT24HOUR: "6.73"
​
CHANGEPCTDAY: "1.90"
​
FROMSYMBOL: "Ξ"
​
HIGH24HOUR: "$ 183.38"
​
HIGHDAY: "$ 183.38"

However, no matter what I try I can't get at it's properties. The object is called 'coinStats'.

I have tried the following:

coinStats.HIGHDAY = undefined
coinStats['HIGHDAY'] = undefined
coinStats["HIGHDAY"] = undefined

Tried to convert it to an array to see if that would help using

Object.values(coinStats)  // Would not work

I am sure the answer is so simplistic. But I am just not sure what it is?

The original raw api results are in the following shape:

(1) […]
​
0: Object { CoinInfo: {…}, RAW: {…}, DISPLAY: {…} }
​
length: 1
​
<prototype>: [

The info I am looking for is in DISPLAY -> USD. I used a map() function to return that sub-object.

The code I am using to fetch the data is essentially the following:

const API = 'https://min-api.cryptocompare.com/data/top/mktcapfull?tsym=USD&page=1';

fetch(API)
  .then(results => results.json())
  .then(coinData => {

    const view = coinData.Data.filter(obj => { return obj.CoinInfo.Name === TRX});

  })

const coinFeedAbridged = view.map(item => item.DISPLAY.USD);

const coinStats = coinFeedAbridged[0];

console.dir(coinStats);

I can't access coinStats.HIGHDAY for example... I get 'undefined'.

13
  • How does your "api call" work? What do you do to retrieve it and what is the shape of the object? Commented May 7, 2019 at 11:58
  • 1
    Try: console.log(coinFeed) and post the output. Commented May 7, 2019 at 11:59
  • That's exactly what I have above. Well... that is from console.dir(coinFeed). Commented May 7, 2019 at 12:07
  • It could be that they are non enumerable, non-writable properties. Check what the output is of Object.getOwnPropertyDescriptor(coinFeed, 'HIGHDAY'). It could also be that the object is frozen: check Object.isFrozen(coinFeed) Commented May 7, 2019 at 12:10
  • 2
    It is hard guessing if we don't have code with which to reproduce the problem. One other thing is that the console also shows inherited properties, which could lead to an explanation. But there can be so many things going on... We need a mvce Commented May 7, 2019 at 12:27

2 Answers 2

1

The fetch call is async, so you can't access the view variable until the call is finished. The code calls the async function and runs the next line, so view variable will be undefined. You have to handle the data inside the fetch callback function, where the data is known.

const API = 'https://min-api.cryptocompare.com/data/top/mktcapfull?tsym=USD&page=1';

  fetch(API)
    .then(results => results.json())
    .then(coinData => {

      const view = coinData.Data.filter(obj => { 
        return obj.CoinInfo.Name === 'TRX'
       });

      const coinFeedAbridged = view.map(item => item.DISPLAY.USD);

      const coinStats = coinFeedAbridged[0];
      console.log(coinStats);

    })
  }

You can test it out in this fiddle. https://jsfiddle.net/gran7ptk/1/

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

6 Comments

I am already able to view the whole object. I just am not able it access it's properties. Although in your Fiddle you used JSON.stringify() and is something I will look into it.
I'm using JSON.stringify() to show it on the screen, you can change it to console.log and you will see the object in the console.
check this out jsfiddle.net/gran7ptk/2, I'm accessing coinStats.HIGHDAY there
That's great but I would like to extract the separate properties within the render() function rather that using setState() to pass one property. What would that code look like?
You can store the whole object in the state and access its properties in the render method. jsfiddle.net/gran7ptk/3
|
0

Change line - Add inverted commas to Text "TRX".

 const view = coinData.Data.filter(obj => { return obj.CoinInfo.Name
 === TRX});

to const view = coinData.Data.filter(obj => { return obj.CoinInfo.Name === "TRX"});

Comments

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.