gpt4 book ai didi

javascript - 迭代 for 循环并将结果附加到 react 组件

转载 作者:行者123 更新时间:2023-12-03 03:36:45 24 4
gpt4 key购买 nike

我很好奇如何迭代数组并将结果附加到 react 组件中。我已经完成了对一系列对象进行排序并获取我想要的帐户的部分。现在我不确定如何将它们附加到 html 中。

export default class SortedTeams extends React.Component {


renderContent(content){
var entries = [{fullname: 'foo', title: 'designer'}, {fullname:
'foo2',
title: 'designer'}, {fullname: 'foo3', title: 'developer'}]
var nohit = [];

for(var i = 0; i <= entries.length - 1; i++) {

if(entries[i].title === 'designer'){
console.log('foohit');
}
else{
nohit.push('nope')
}
}

render() {
var content = this.props.block.content;


return(
<SiteBuilderBlock tag="section" className="sortedTeams" block={this.props.block}>
<div>
<SiteBuilderList tag="div" list="teams" inGroupsOf={4} inGroupsOfClassName="row">
{(element, key) => this.renderContent(element, key)}
</SiteBuilderList>
</div>

</SiteBuilderBlock>
);

}

}

最佳答案

使用.map()迭代数组。在本例中,contacts 是一个对象数组,例如entries。这里我使用 ES6 语法和 JSX

The map() method creates a new array with the results of calling a provided function on every element in the calling array.

return(
<div>
<ul>
{ contacts.map(contact => (
<li key={contact.id}>
{contact}
</li>
)
)}
</ul>
</div>
);

关于javascript - 迭代 for 循环并将结果附加到 react 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45822418/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com