简体   繁体   English

如何在React.js中循环返回复杂的JSON?

[英]How to loop over a complex JSON in return in Reactjs?

i am learning Reactjs while building a simple application. 我在构建一个简单的应用程序时正在学习Reactjs。 I know this is very basic and there are alot of answers for the same question available on stackoverflow. 我知道这是非常基本的,并且对于stackoverflow上的同一问题有很多答案。 But i am unable to implement those maybe because of variable scopes. 但由于范围可变,我无法实现这些功能。 I would be grateful if someone is able to help me out with this. 如果有人能够帮助我,我将不胜感激。

My full code is pretty complex and irrelevant to the question. 我的完整代码非常复杂,与问题无关。 But please refer to this fiddle : Full Code . 但是请参考以下小提琴: 完整代码 This will work in local server. 这将在本地服务器上工作。

Relevant Code: 相关代码:

var localStore = {};
// localStore = {"f":{"symbol":"f","quantity":10,"pricePaid":136},"jnj":{"symbol":"jnj","quantity":30,"pricePaid":146}};

var SearchStock = React.createClass({
...
render: function() {
...
return ( // I tried map function in other ways too like [localstore].map and then get its values in similar way as i am getting below.
{/*localStore.map(function (l, i) {
  return <tr>
           <td>{Object.keys(l)[i].symbol}</td>
           <td>{Object.keys(l)[i].quantity}</td>
           <td>{Object.keys(l)[i].pricePaid}</td>
           <td>{Object.keys(l)[i].symbol}</td>
         </tr>
})*/}
{/*for(var x in localStorage){ // This is what i really want to do.
  return <tr>
           <td>{x.symbol}</td>
           <td>{x.quantity}</td>
           <td>{x.pricePaid}</td>
           <td>anything</td>
         </tr>
}*/}
{stocks.map(function(l) { // this works.
   console.log("in return stocks",JSON.stringify(stocks));
   return <tr>
            <td>{l[Object.keys(l)[0]]["name"]}</td>
            <td>{this.state.quantity}</td>
            <td>{this.state.pricePaid}</td>
            <td>{<input type="button" value="View Stock"/>}</td>
          </tr>
}, this)}

I don't know why map function didn't work as value in localStore and stocks is similar: stocks = [{only one key pair of localStore}] . 我不知道为什么map函数不能在localStore中用作值,而stocks却是类似的: stocks = [{only one key pair of localStore}] I would appreciate any help or guidance i can get for this. 我将不胜感激,为此我可以提供任何帮助或指导。

You should use Object.keys to get an array with the keys of localStore and iterate through it to display your data. 您应该使用Object.keys来获取带有localStore键的数组,并对其进行迭代以显示数据。

{Object.keys(localStore).map(function (key) {
          return <tr key={key}>
              <td>{localStore[key].symbol}</td>
              <td>{localStore[key].quantity}</td>
              <td>{localStore[key].pricePaid}</td>
              <td>{localStore[key].symbol}</td>
          </tr>
 })}

You were missing other things like 您错过了其他东西,例如

1º Provide a key to every child in the array. 1º为阵列中的每个孩子提供一个钥匙。

2º Wrap your <tr> tags inside a <tbody> . 2º将<tr>标记包装在<tbody> <tr> cannot appear as a child of <table> <tr>不能作为<table>的子代出现

jsfiddle jsfiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM