-1

I'm trying to access and process the response of an API call in this form:

{ BTC: { available: '0.77206464', onOrder: '0.00177975' },
  LTC: { available: '0.00000000', onOrder: '0.00000000' },
  ETH: { available: '1.14109900', onOrder: '0.00000000' },
  BNC: { available: '0.00000000', onOrder: '0.00000000' },
  MTL: { available: '0.00000000', onOrder: '0.00000000' },
  SALT: { available: '0.00000000', onOrder: '0.00000000' },
  NULS: { available: '0.00000000', onOrder: '0.00000000' } }

I'm using js and node for the backend server and ejs as templating.

Trying to iterate over this object always get me something like

BTC [object Object] LTC [object Object]

Anyone could help me? Thanks in advance

phuzi
  • 12,078
  • 3
  • 26
  • 50
marte3707
  • 3
  • 1
  • What are you _expecting_? – Ivar May 18 '21 at 16:13
  • Help you what? Dot or bracket notation should do the job just fine. – isherwood May 18 '21 at 16:13
  • "Trying to iterate over this object" - we don't know what this means. Please read [how to ask](https://stackoverflow.com/help/how-to-ask) especially the [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Andy Ray May 18 '21 at 16:15
  • You haven't showed us the code you're using to iterate and work with the items. You're likely doing a for/in and alerting or dumping to screen the top level key/values--treating them as strings. So you're seeing the key, and the string conversion of the object in the value. – JAAulde May 18 '21 at 16:17

1 Answers1

0

I think you might be looking for that.

var objOfobj = { BTC: { available: '0.77206464', onOrder: '0.00177975' },
  LTC: { available: '0.00000000', onOrder: '0.00000000' },
  ETH: { available: '1.14109900', onOrder: '0.00000000' },
  BNC: { available: '0.00000000', onOrder: '0.00000000' },
  MTL: { available: '0.00000000', onOrder: '0.00000000' },
  SALT: { available: '0.00000000', onOrder: '0.00000000' },
  NULS: { available: '0.00000000', onOrder: '0.00000000' }  
}
  
  

for (const property in objOfobj) {
  console.log(`${property}: ${objOfobj[property].available}`);
  console.log(`${property}: ${objOfobj[property].onOrder}`);
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
crg
  • 4,284
  • 2
  • 29
  • 57
  • It worked! Thanks. One question what is the ` used for? – marte3707 May 18 '21 at 16:29
  • I'm glad my answer solved you problem, do not forget to validate to let the community know that your problem is solved. – crg May 18 '21 at 16:39
  • The ` is a syntax to use variable with ${} directly in a string. If you prefer use `console.log(property + " : " + objOfobj[property].onOrder)` – crg May 18 '21 at 16:42