javascript - 循环遍历map函数
问题描述
我正在使用 map 函数循环遍历数组以返回 HTML 选择标签的选项标签。但这似乎不起作用。Project_titles 数组已正确填充数据。
我在其他地方使用了相同的代码,它在那里工作。
render() {
<select
id="sel4"
onChange={event => this.setState({ project: event.target.value })}
>
{this.func()}
</select>;
}
func() {
this.state.project_titles.map(function(title, i) {
return (
<option key={i} value={title}>
{title}
</option>
);
});
}
选择标签应该填充选项,但它是空的。
解决方案
这行得通。您的代码的问题是您没有从 func() 函数返回最终的 Options 数组。
render(){
<select
id="sel4"
onChange={event => this.setState({ project: event.target.value })}
>
{this.func()}
</select>;
};
func = () => {
return this.state.project_titles.map(function(title, i) {
return (
<option key={i} value={title}>
{title}
</option>
);
});
};
推荐阅读
- javascript - jquery如何比较两个数组
- azure-active-directory - 用户从 Azure AD 中删除后保持登录状态
- c++ - 具有局部 y 方向的向量与另一个向量的叉积
- jquery - jQuery:如何找到一个数字是浮点数还是整数?
- shell - 如何在unix的所有子文件夹中查找特定文件
- json - 将检索到的 json 转换为颤振中的自定义对象列表
- javascript - 为什么 ngIf 不适用于类属性
- android - OnPause() 中的公共字符串数组
- windows - 如何检查文件扩展名?
- mysql - MYSQL如何将当前余额查询为以前的余额