简体   繁体   English

如何使用reactjs将对象数组打印到表中?

[英]How can I print array of object into my table using reactjs?

I just want to print array which I get from the firebase. 我只想打印从firebase获得的数组。 You can see in the image the array data. 您可以在图像中看到阵列数据。

In which I convert json object data into the array easily, but I cannot print the array data in table. 在其中我可以轻松地将json对象数据转换为数组,但无法在表中打印数组数据。 If I run console.log command on tag that time it print the console data, but not print the array value that I get from the firebase or you can say fetch from the firebase. 如果我当时在标签上运行console.log命令,它将打印控制台数据,但不打印我从firebase获得的数组值,或者可以说从firebase获取。 I want to print these data in table. 我想在表中打印这些数据。

在此处输入图片说明

 class Contact extends React.Component { constructor() { super(); this.state = { arr: [], inner_key:[], Address:'', printabledata:[], singledata:{} }; self = this; this.printAllRecord = this.printAllRecord.bind(this); } componentWillMount() { var formdata2 = firebase.database().ref("vishal-aaede/"); formdata2.on("value", function (snap) { data = snap.val(); self.setState({arr: data}); }); } componentDidUpdate(){ var data=this.state.arr, headerArray=[]; for (var key in data){ headerArray.push(key); }console.log(headerArray); headerArray.forEach(function(val){ self.printAllRecord(data[val]) console.log(data[val].Name); } ); self.state.singledata=data[headerArray[0]] ; } printAllRecord(param,index){ self.state.printabledata.push(<tr> <td>{param.Name}</td> <td>{param.Round}</td> <td>{param.Email}</td> <td>{param.Date}</td> <td>{param.Phone}</td> <td>{param.Address}</td> <td>{param.Fresher}</td> <td>{param.Time}</td> </tr>); } render() { return ( <div> <h2>Candidate RECORD Coming Soon!!!!</h2> <p>Write Filter Code here </p> <div> <div className="row"> <div className="col-sm-2"></div> <div className="col-sm-8"> <div className = "container-fluid table-responsive"> <table className = "table table-bordered"> <thead> <tr> <th>Name</th> <th>Interview Round</th> <th>Email</th> <th>Date</th> <th>Phone Number</th> <th>Address</th> <th>Gender</th> <th>Experience</th> <th>Time</th> </tr> </thead> <tbody> {this.state.printabledata} </tbody> </table> </div> </div> <div className="col-sm-2"></div> </div> </div> </div> ) } } 

You are repeating elements with the wrong approach. 您使用错误的方法来重复元素。 If you want to loop through array elements then you should use array.map methods. 如果你想loop通过array元素,那么你应该使用array.map方法。 You can find some reference here for how we print a list of elements in react . 您可以在此处找到一些有关我们如何在react中打印元素列表的参考。

You can do it using map 您可以使用地图来做

this.state.data.map(function(key,map) {
    <table>
        <tr></tr>
        <td>{map.name}</td?
        ....whole code
    </table>
})

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

相关问题 如何打印出保存在“联系人”数组中的每个对象 - How can i print out every object that i saved in my “contacts” array 如何在 ReactJS 的对象中添加更多元素? - How can i add more elements in my object in ReactJS? ReactJs 如何在不发生变异的情况下将对象添加到数组? - ReactJs how can i add object to array without mutating? 如何在 ReactJS 中按 object arrays 过滤数组项? - How can I filter array items by object arrays in ReactJS? 我如何在 reactjs 中正确地通过这个数组 object map? - how can i map through this array object properly in reactjs? 我如何访问对象内的数组元素并将它们全部打印在引导表中? - how can i access array element inside an object and print them all in a bootstrap table? 如何使用 JavaScript 访问数组中的特定对象? - How can I access a specific object in my array using JavaScript? 如何在 Visual Studio Code 应用程序中使用 ReactJS 以表格格式打印此数组元素 - How to print this array elements in table format using ReactJS in Visual Studio Code Application 我如何获取 get api 调用的数据,格式为 reactjs 中 object 数组中的 object 嵌套数组? - How can i fetch the data of an get api call which is in the format as nested array of object in array of object in reactjs? 如何在表格左侧打印值? - How can I print values to the left of my table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM