iterating nested js objects

Viewed 58

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.stack.imgur.com/eK8Ic.png

these are my output successful tries

https://i.stack.imgur.com/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.

3 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']

Have you tried for..in loop?, you can loop object with for...in. You can read here

const parent = {
  "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
  }
}

for (const key in parent) { // This is just O(n)
  // key - BTC,ETH
  console.log(parent[key].price)
  console.log(parent[key].percent_change_24h)
}

You were on the right track with Object.keys. There's another method called Object.values that gives you the values for each key of the object.

If our object is data, then Object.keys(data) will give us a two-value array/. Afterwards, we could, for example iterate with map() to get all prices:

Object.values(o).map(d => d.price)

let o = {
  "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(Object.values(o))

Related