简体   繁体   English

Map 在 React 中的数组上

[英]Map over an array in React

I am building an app that displays video games and various info about them, pulling data from an API. I am trying to display all of the platforms that the game is playable on, PlayStation, Xbox, etc. The JSON has an array of platforms for each game that each contain a platform object with a name (that I am trying to display).我正在构建一个显示视频游戏和有关它们的各种信息的应用程序,从 API 中提取数据。我正在尝试显示可玩该游戏的所有平台,PlayStation、Xbox 等。JSON 有一系列平台对于每个游戏,每个游戏都包含一个平台 object 和一个名称(我试图显示)。 Maps really confuse me I would really appreciate someone's help with this.地图真的让我感到困惑我真的很感谢有人对此的帮助。 I will include a snippet of the JSON.我将包括 JSON 的一个片段。

  "count": 1326,
  "next": "https://api.rawg.io/api/games?key=8d1a421af80b4f9b8650de12d9943010&page=2&search=destiny",
  "previous": null,
  "results": [
    {
      "slug": "destiny",
      "name": "Destiny",
      "playtime": 24,
      "platforms": [
        {
          "platform": {
            "id": 1,
            "name": "Xbox One",
            "slug": "xbox-one"
          }
        },
        {
          "platform": {
            "id": 18,
            "name": "PlayStation 4",
            "slug": "playstation4"
          }
        },
        {
          "platform": {
            "id": 14,
            "name": "Xbox 360",
            "slug": "xbox360"
          }
        },
        {
          "platform": {
            "id": 16,
            "name": "PlayStation 3",
            "slug": "playstation3"
          }
        }
      ],
const GameCard = ({
  game: {
    name,
    background_image,
    metacritic,
    released,
    platforms,
    platform,
    esrb_rating,
  },
}) => {
  return (
    <div className="bg-gray-600/30 m-5 p-0 rounded-xl shadow-xl shadow-gray-700 border-solid border-2 border-gray-700 max-w-[640px] hover:scale-105 ease-in duration-300">
      <img
        className="rounded-t-xl h-[360px] w-[640px] border-b-2 border-gray-600"
        alt={name}
        src={
          background_image
            ? background_image
            : "https://via.placeholder.com/640x360"
        }
      />
      <h1 className="text-center text-4xl text-gray-900 tracking-wide font-bold mt-2 font-mono">
        {name}
      </h1>
      <div className="text-gray-800 text-center font-mono py-2">
        {platforms ? platforms.map(() => <span></span>) : null}

Per W3Schools :根据W3Schools

map() creates a new array from calling a function for every array element. map() 通过为每个数组元素调用 function 创建一个新数组。

map() calls a function once for each element in an array. map() 为数组中的每个元素调用一次 function。

map() does not execute the function for empty elements. map() 不会对空元素执行 function。

map() does not change the original array. map() 不会改变原始数组。

This creates an array of names from an array of platform objects, such as results.platforms in the json snippet you showed.这会从一组平台对象创建一组名称,例如您显示的 json 片段中的 results.platforms。

platforms.map(platform => <span>{platform.platform.name}</span>)

If you map over the platform after taking that part to the variable called platforms in your code then you can map over it like I shown below:如果你在将那部分带到代码中称为平台的变量之后在平台上 map,那么你可以在它上面 map,如下所示:

 <div className="text-gray-800 text-center font-mono py-2"> {platforms? platforms.map((item) => <span key={item.platform.id}> {item.platform.name} </span>): null } </div>

Don't forget to insert the key inside the span tag.不要忘记将键插入 span 标签内。 Which will act as a unique identifier for each span tag.它将作为每个 span 标签的唯一标识符。

reference: Lists and Keys参考:列表和键

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

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