简体   繁体   English

循环遍历对象,将值拆分为自己的对象以传递到车把

[英]Loop through object, split value into its own object to pass to Handlebars

I am making an app where I make use of an API which returns data such as the recent_form of a team (football/soccer) - recent form returns something like WLDWW (meaning win/draw/loss) and I want to split that and make it into it's own object for when I output it into my handlebars (this way I can do background colours and eventually expand on functionality) 我正在制作一个应用程序,在其中我使用了一个API,该API返回的数据如球队的last_form(足球/足球)-最近的表单返回WLDWW之类的东西(意思是赢/输/输),我想将其拆分并制作当我将其输出到车把中时,它就变成了它自己的对象(这样我就可以做背景色并最终扩展其功能)

I currently have a for loop which goes through all the different teams in the table and splits the recent form into an object, however I get the issue that when I pass that object into my res.render it only sends over the last one and not all 20 of them. 我目前有一个for循环,遍历表中的所有不同团队,并将最近的表单拆分为一个对象,但是我遇到的问题是,当我将该对象传递给res.render时,它仅发送最后一个而不发送全部20个。

I'll show how my current app is set up (although not working): 我将展示如何设置当前应用(尽管无法正常工作):

 router.get('/table', (req, res, next) => { var url = `http://api.football-api.com/2.0/standings/1204${config.auth}` axios.get(url).then(response => { const posSort = response.data.sort((a, b) => { return a.position - b.position; }); for(var i = 0, l = posSort.length; i < l; i++) { var recentForm = posSort[i].recent_form.split(''); }; console.log(recentForm); res.render('table', { teamStats: posSort, recentForm: recentForm }); }).catch(error => { console.log(error); }) }); 
 <table> <tr> <th>Position</th> <th>Club</th> <th>Played</th> <th>Won</th> <th>Drawn</th> <th>Lost</th> <th>GF</th> <th>GA</th> <th>GD</th> <th>Points</th> <th>Form</th> </tr> {{#each teamStats}} <tr> <td>{{position}}</td> <td>{{team_name}}</td> <td>{{overall_gp}}</td> <td>{{overall_w}}</td> <td>{{overall_d}}</td> <td>{{overall_l}}</td> <td>{{overall_gs}}</td> <td>{{overall_ga}}</td> <td>{{gd}}</td> <td>{{points}}</td> {{/each}} {{#each recentForm}} <td></td> {{/each}} </tr> </table> 

Just to run down what is happening above in a tldr; 只是为了耗尽tldr中上面发生的一切; I get the data from the API, I sort it based on the position field (posSort variable), then I split the recent_form value so that it is an object, this is what I attempt to pass to my res.render - handlebars is intended to loop through recentForm 我从API获取数据,然后根据position字段(posSort变量)对数据进行排序,然后拆分Latest_form值,使其成为一个对象,这就是我尝试传递给res.render的内容-handlebars遍历最近的表单

The reason why you only see the last item in the 'recentForm' variable is because the value of 'recentForm' gets overwritten in every iteration of the for-loop. 之所以只看到'recentForm'变量中的最后一项,是因为在for循环的每次迭代中'recentForm'的值都会被覆盖。

What you want to do is concatenate or push the value of 'posSort[i].recent_form.split('')' in every iteration, instead of assigning new values to the variable. 您想要做的是在每次迭代中串联或推送'posSort [i] .recent_form.split('')'的值,而不是为变量分配新值。

var recentForm = [];

for(var i = 0, l = posSort.length; i < l; i++) {
  recentForm = recentForm.concat(posSort[i].recent_form.split(''));
};

You can alternatively map the 'posSort' to a new array using ES6 map method. 您也可以使用ES6映射方法将“ posSort”映射到新数组。

var recentForm = posSort.map(pos => pos.recent_form.split(''));

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

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