简体   繁体   English

从React.js中的状态数组渲染特定值

[英]Rendering Specific Value from State Array in React.js

I am creating an online scorecard application for a 9-hole golf course using React.js, State, and rendering. 我正在使用React.js,State和渲染为9洞高尔夫球场创建在线记分卡应用程序。 A user enters his or her score for each hole, which 'pushes' hole number & hole score to an array (known as golferOneScore) in the State. 用户输入他或她对每个洞的分数,该分数会将“洞数”和“洞数”“推”到该州的一个数组(称为golferOneScore)中。 Rendering to the browser is proving difficult, though, for display of each hole's score in attempting to render via map method based on value of hole in the array... 但是,要想通过数组方法基于数​​组中的孔的值尝试通过map方法显示每个孔的分数,向浏览器进行渲染非常困难...

I've tried mapping out the array (specifically for one hole number) in my html area. 我试着在我的html区域中绘制出阵列(特别是一个孔号)。 It works, but doing so, though, is limiting display to only one hole per map. 它可以工作,但是这样做限制了每个地图只能显示一个孔。 Upon time to map for the score on hole 2, I'm losing display of my entry in the browser for hole 1. 随着时间在洞2上映射分数,我丢失了在洞1中在浏览器中显示的条目。

{this.state.golferOneScore.map((data) =>  {
    if (data.hole == 1) { 
        return (<tr><td>golferOne</td><td>{data.score}</td></tr>)
        })};

Expecting to display in all 9 holes of scores accurately, even if a golfer should, for any reason, skip a hole. 即使高尔夫球手出于任何原因应跳过一个洞,也希望可以在9个洞中准确显示分数。

Learned I could simply use a find method for each hole golfed 了解到我可以简单地对每个打高尔夫球的洞使用查找方法

{this.state.nameOneScore.find(scores => scores.hole === '1').score}

ternary condition to control for null is important, too. 控制null的三元条件也很重要。

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

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