react版数组遍历

时间: 2023-07-11 admin 互联网

react版数组遍历

react版数组遍历

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><title>Title</title>
</head><body><!--创建容器--><div id="app"></div></body></html>
<!-- 引入react核心 -->
<script src="@16/umd/react.development.js" crossorigin></script>
<!-- 引入react-dom,用于支持react操作dom -->
<script src="@16/umd/react-dom.development.js" crossorigin></script>
<!-- 引入babel,用于将jsx转为js -->
<script src="/@babel/standalone/babel.min.js"></script><script type="text/babel"> //此处一定要写babel//创建数组let arr = [{ name: "周杰伦", music: "七里香" },{ name: "林俊杰", music: "江南" },{ name: "那英", music: "征服" },{ name: "蔡徐坤", music: "情人" }];//1.创建虚拟domlet vDOM = (<div><ul><li>姓名: <strong>周杰伦</strong>成名曲 : <strong>七里香</strong></li><li>姓名: <strong>林俊杰</strong>成名曲 : <strong>江南</strong></li><li>姓名: <strong>那英</strong>成名曲 : <strong>征服</strong></li><li>姓名: <strong>周杰伦</strong>成名曲 : <strong>七里香</strong></li></ul></div>)//2.将创建好的虚拟dom渲染到容器ReactDOM.render(vDOM, document.getElementById("app"))</script>

react升级版数组遍历

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><title>Title</title>
</head><body><!--创建容器--><div id="app"></div></body></html>
<!-- 引入react核心 -->
<script src="@16/umd/react.development.js" crossorigin></script>
<!-- 引入react-dom,用于支持react操作dom -->
<script src="@16/umd/react-dom.development.js" crossorigin></script>
<!-- 引入babel,用于将jsx转为js -->
<script src="/@babel/standalone/babel.min.js"></script><script type="text/babel"> //此处一定要写babel//创建数组let arr = [{ name: "周杰伦", music: "七里香" },{ name: "林俊杰", music: "江南" },{ name: "那英", music: "征服" },{ name: "蔡徐坤", music: "情人" }];//1.创建虚拟dom -- jsx语法let vDOM = (<div><ul>{arr.map((item, index) =><li key={index}>姓名: {item.name} --- 音乐: {item.music}</li>)}</ul></div>);//2.将创建好的虚拟dom渲染到容器ReactDOM.render(vDOM, document.getElementById("app"))</script>