1

How do i loop through this array and display the key ("ETH_SAN", "ETH_LINK") into components?

{
  ETH_SAN: 
   { last: '0.000981',
     high: '0.0010763',
     low: '0.0009777',
     lowestAsk: '0.00098151',
     highestBid: '0.0007853',
     percentChange: '-1.83619353',
     baseVolume: '7.3922603247161',
     quoteVolume: '7462.998433' },
  ETH_LINK: 
   { last: '0.001',
     high: '0.0014',
     low: '0.001',
     lowestAsk: '0.002',
     highestBid: '0.001',
     percentChange: '-28.57142857',
     baseVolume: '13.651606265667369466',
     quoteVolume: '9765.891979953083752189' }
  // all possible markets follow ...
}

So it prints out this:

<Text>ETH_SAN</Text>
<Text>ETH_LINK</Text>
4
  • 1
    This is not an array. Commented May 16, 2018 at 11:31
  • 1
    something like: Object.keys('yourObjectName').map((key) => <Text>{key}</Text>)? Commented May 16, 2018 at 11:34
  • that worked @gaback - Thanks! Commented May 16, 2018 at 11:49
  • Great, that's help! @Nickmcoomb Mikhail Katrin is with the same solution and he saw this just like me and took time to write the code. Could you accept his answer? Thanks! Commented May 16, 2018 at 12:00

2 Answers 2

1

You could use Object.keys()

const obj = {
  ETH_SAN: 
   { last: '0.000981',
     high: '0.0010763',
     low: '0.0009777',
     lowestAsk: '0.00098151',
     highestBid: '0.0007853',
     percentChange: '-1.83619353',
     baseVolume: '7.3922603247161',
     quoteVolume: '7462.998433' },
  ETH_LINK: 
   { last: '0.001',
     high: '0.0014',
     low: '0.001',
     lowestAsk: '0.002',
     highestBid: '0.001',
     percentChange: '-28.57142857',
     baseVolume: '13.651606265667369466',
     quoteVolume: '9765.891979953083752189' }
  // all possible markets follow ...
}

const keys = Object.keys(obj);

console.log(keys);

// add following line in your render() method
//Object.keys(obj).map(key => <Text>{key}</Text>)

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

Comments

1

Use this below

 const data = {
      ETH_SAN: 
       { last: '0.000981',
         high: '0.0010763',
         low: '0.0009777',
         lowestAsk: '0.00098151',
         highestBid: '0.0007853',
         percentChange: '-1.83619353',
         baseVolume: '7.3922603247161',
         quoteVolume: '7462.998433' },
      ETH_LINK: 
       { last: '0.001',
         high: '0.0014',
         low: '0.001',
         lowestAsk: '0.002',
         highestBid: '0.001',
         percentChange: '-28.57142857',
         baseVolume: '13.651606265667369466',
         quoteVolume: '9765.891979953083752189' }
      // all possible markets follow ...
    }

//Step 1:    
    for(key in data){
       <Text>{key}</Text>
    }
    or
//Step 2:
   Object.keys(myObj).forEach(function (key) {
    let obj = myObj[key];
     <Text>{key}</Text>
    // do something with obj
  });

https://esdiscuss.org/topic/es6-iteration-over-object-values

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.