简体   繁体   English

如何在 React 中循环遍历数组?

[英]How to loop through arrays in React?

How to loop through arrays in reactjs using map function?如何使用map函数在reactjs中循环数组?

shop: [
{id: 35, name: 'jumper', color: 'red', price: 20},
{id: 42, name: 'shirt', color: 'blue', price: 15},
{id: 56, name: 'pants', color: 'green', price: 25},
{id: 71, name: 'socks', color: 'black', price: 5},
{id: 72, name: 'socks', color: 'white', price: 5},]

<ul><li>[array loop]</li></ul>

Have you used the map function before?你以前用过地图功能吗?

The map() method creates a new array with the results of calling a provided function on every element in the calling array. map() 方法使用对调用数组中的每个元素调用提供的函数的结果创建一个新数组。 So the map function was introduced to Javascript in ES2015.所以地图函数在 ES2015 中被引入到 Javascript 中。 It greatly simplifies the looping process and cuts out the need to use a simply for loop or a forEach function.它极大地简化了循环过程,并且不再需要使用简单的 for 循环或 forEach 函数。

<ul>
    {shop.map((item, key) =><li item={item} key={item.id}></li>)}
</ul>
<ul>
  {shop.map((item, index) => <li key={item.id}>{item.name}</li>}
</ul>

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

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