简体   繁体   English

无法通过数组访问所需信息 object javascript

[英]Having trouble accessing required info through array object javascript

我要访问的内容的图片

A more detailed picture更详细的图片

I am trying to access platform.name for all the parent_platforms however i am getting a bunch of commas in return.我正在尝试访问所有 parent_platforms 的 platform.name 但是我得到了一堆逗号作为回报。 I am pretty sure i am supposed to use the.map function to iterate through all platform.name in the parent_platform.我很确定我应该使用 .map function 来遍历 parent_platform 中的所有 platform.name。 Basically my desired output would be names of all the platforms like this PC, PlayStation, Xbox Here is my current code so far.基本上我想要的 output 将是所有平台的名称,如这台 PC、PlayStation、Xbox 这是我目前的代码。 Thanks谢谢

function showGames(game)
{
  game.forEach(game => {
    console.log(game);
    const card = document.createElement('div');
    card.classList.add('card');
    card.innerHTML = `
    <div class="card-top">
      <img src="${game.background_image}" alt="" class="card-image">
    </div>
    <div class="card-bottom">
      <h1>${game.name}</h1>
      <h3>Release date: ${game.released}</h3>
      <p>Platforms: ${game.parent_platforms.map(platform => platform.name)}</p>
      <p>Rating: ${game.rating}</p>
    </div>
    `;
    main.appendChild(card);
  })
}

Your use of map will give an array of the platform names, but what then you'll get a standard array representation (conversion to string) which is unlikely to be what you actually want in the HTML representation.您对 map 的使用将给出一个平台名称数组,但是您将获得一个标准数组表示形式(转换为字符串),这不太可能是您在 HTML 表示形式中实际想要的。

You might like to try using Array.join, like this:您可能想尝试使用 Array.join,如下所示:

${game.parent_platforms.map(platform => platform.name).join()}

This will insert a string formatted such as "PC,PlayStation,Xbox" into the HTML output. If you want a different separator, add it as an argument to join, thus:这会将格式为“PC,PlayStation,Xbox”的字符串插入到 HTML output 中。如果您想要不同的分隔符,请将其添加为参数以加入,因此:

${game.parent_platforms.map(platform => platform.name).join(', ')}

The structure of the rest of the code is problematic as other commenters have pointed out, but this answers your question as to use of map.正如其他评论者指出的那样,代码 rest 的结构存在问题,但这回答了您关于使用 map 的问题。

As @danh mentions, there is also a missing level of dereferencing (I'm renaming the parent_platforms element to make it clear):正如@danh 所提到的,还缺少级别的取消引用(我正在重命名 parent_platforms 元素以使其清楚):

${game.parent_platforms.map(element => element.platform.name).join()}

If this is what needs to be mapped...如果这是需要映射的...

[{"platform":{"id":1,"name":"PC","slug":"pc"}},{"platform":{"id":2,"name":"PlayStation","slug":"playstation"}},{"platform":{"id":3,"name":"Xbox","slug":"xbox"}}]

and this is the mapping code...这是映射代码...

game.parent_platforms.map(platform => platform.name)

You will certainly get an array of nulls.你肯定会得到一个空数组。 There's another level of object nesting there, as follows...那里还嵌套了一层object,如下...

game.parent_platforms.map(platform => platform.platform.name)
//                                             ^^^^^^^^

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

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