React Native - 循环键值数组

时间:2018-05-16 11:30:35

标签: javascript reactjs react-native ecmascript-6

如何遍历此阵列并将键(“ETH_SAN”,“ETH_LINK”)显示到组件中?

{
  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 ...
}

所以打印出来:

<Text>ETH_SAN</Text>
<Text>ETH_LINK</Text>

2 个答案:

答案 0 :(得分:0)

您可以使用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>)

答案 1 :(得分:0)

使用以下

 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