
react中怎么遍历数组?
react采用forEach或map两种方式遍历数组
forEach() 方法用于调用数组的每个元素,并将元素传递给回调函数。
map() 方法返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值。
map() 方法按照原始数组元素顺序依次处理元素。
forEach
import React,{Component} from 'react';
let list=[
{
name:"百度",
address:"http://www.baidu.com"
},
{
name:"google",
address:"http://www.google.cn"
},
{
name:"firefox",
address:"https://home.firefoxchina.cn"
}
];
class forEach extends Component{
render(){
//定义一个数组,将数据存入数组
const elements=[];
list.forEach((item)=>{
elements.push(
<div>
{item.name}
<a>{item.address}</a>
<hr/>
</div>
)
});
return(
<div>
{elements}
</div>
)
}
}
export default forEach;
map
import React,{Component} from 'react';
let list=[
{
name:"百度",
address:"http://www.baidu.com"
},
{
name:"google",
address:"http://www.google.cn"
},
{
name:"firefox",
address:"https://home.firefoxchina.cn"
}
];
class forEach extends Component{
render(){
return(
list.map((item)=>
<div>
{item.name}
<a>{item.address}</a>
<hr/>
</div>
)
)
}
}
export default forEach;
更多web前端开发知识,请查阅 HTML中文网 !!
以上就是react中怎么遍历数组?的详细内容,更多请关注易知道|edz.cc其它相关文章!













