Home > Enterprise >  How to access differing API data from react app
How to access differing API data from react app

Time:10-11

trying to build a react app that outputs currency information from an API but the field is different for each currency. for example, for the united states currency is listed as:

currency:{
    USD:{
     name: "United States Dollar"
     symbol: "$"
   }
}

whereas croatian currency is listed as:

currency:{
    HRK:{
     name: "Croatian kuna"
     symbol: "kn"
   }
}

To access the usd, I would normally use data.currency.usd.name or data.currency.usd.name but the "usd" aspect of this changes everytime. Just asking how would I make it so that part of the code is flux/ignores the specific field of the currency and just outputs the name/symbol. Any help is appreciated as I am fairly new at react and I am willing to answer any questions. This is in html btw.

CodePudding user response:

let currency = {
  USD: {
    "name": "UnitedStates Dollar",
    "symbol": "$"
  }
}

console.log(Object.values(currency))

Output : [ { name: 'UnitedStates Dollar', symbol: '$' } ]

  • Related