Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iterating nested js objects

i've had trouble accessing this Object :

{
  "BTC": {
    "price": 13322.848306438736,
    "percent_change_24h": 2.00659441,
    "market_cap": 246870460628.15366,
    "volume_24h": 33956399259.1725,
    "circulating_supply": 18529856,
    "total_supply": 18529856,
    "max_supply": 21000000
  },
  "ETH": {
    "price": 380.8150073343507,
    "percent_change_24h": -0.67132589,
    "market_cap": 43115533610.355606,
    "volume_24h": 15706287212.275429,
    "circulating_supply": 113219103.1865,
    "total_supply": 113219103.1865,
    "max_supply": null
  }
}

and so far i've tried Object.key() , map(), making it an array, algorithmic methods and so on. i could get the list of outer objects by :

Object.keys(this.state.dataApi).map((value,key)=>(
                   value
                  ))

i could get what's inside "BTC" for example as an object by :

this.state.dataApi.BTC

but i wasn't able to get its inner elements one by one. this is the expected output example

https://i.sstatic.net/eK8Ic.png

these are my output successful tries

https://i.sstatic.net/AK6lR.png

Note: this also tried changing type from the state [] , {} , "" , only "" got me a bit somewhere as it leaves the object in its original type.

like image 732
EagleMind Avatar asked May 16 '26 14:05

EagleMind


1 Answers

let obj= {
  "BTC": {
    "price": 13322.848306438736,
    "percent_change_24h": 2.00659441,
    "market_cap": 246870460628.15366,
    "volume_24h": 33956399259.1725,
    "circulating_supply": 18529856,
    "total_supply": 18529856,
    "max_supply": 21000000
  },
  "ETH": {
    "price": 380.8150073343507,
    "percent_change_24h": -0.67132589,
    "market_cap": 43115533610.355606,
    "volume_24h": 15706287212.275429,
    "circulating_supply": 113219103.1865,
    "total_supply": 113219103.1865,
    "max_supply": null
  }
};

console.log(obj["BTC"]);
console.log(obj["ETH"].price);

Object.keys(obj).map((value,key)=>{
   console.log(obj[value]);
   console.log(obj[value].price);
}
                  )

You were right. But you need access object using['value']

like image 183
Victor Avatar answered May 19 '26 03:05

Victor