简体   繁体   English

如何使用 Javascript 简单地遍历数组?

[英]How can I simply loop through an array using Javascript?

I have a dataset returned by a third party API as JSON.我有一个由第三方 API 作为 JSON 返回的数据集。

Now I want to loop through the data and populate a league table at my frontend (using 11 key:values from the array).现在我想遍历数据并在我的前端填充一个排行榜(使用数组中的 11 个键:值)。

I already transformed the object into an array (var standings ) and defined an empty variable "rank".我已经将 object 转换为数组(var standings )并定义了一个空变量“rank”。 But now I am really stuck on how to proceed, other tutorials just increase my confusion.但现在我真的被困在如何继续下去,其他教程只会增加我的困惑。

Do I need to create eleven empty arrays to grab the required data into them and populate the html using the "new" arrays afterwards?我是否需要创建 11 个空的 arrays 来获取所需的数据,然后使用“新的”arrays 填充 html? Probably this task can be handled by a "25-line-all-in-super-loop-solution".可能这个任务可以通过“25-line-all-in-super-loop-solution”来处理。

This is my Javascript (applause:):这是我的 Javascript(掌声:):

          $.ajax({
            method: "GET",
            async: "True",
            dataType: "json",
            url: "https://cors-anywhere.herokuapp.com/https://www.api-football.com/demo/api/v2/leagueTable/" + league_id,
            success: function(response) {

              var standings = response.api.standings;
              for (let i = 0; i < standings.length; i++) {

                var rank = [];

                  console.log(standings[i].teamName);
                }

The console.log returns undefined (i tried to print all 20 teamnames within the array). console.log 返回undefined (我试图打印数组中的所有 20 个团队名称)。

This is the JSON data (it returns 1 result = 1 league table including all teams in the array with additional data)这是 JSON 数据(它返回 1 个结果 = 1 个联赛表,包括数组中的所有球队以及其他数据)

{
    "api": {
        "results": 1,
        "standings": [
            [
                {
                    "rank": 1,
                    "team_id": 85,
                    "teamName": "Paris Saint Germain",
                    "logo": "https://media.api-football.com/teams/85.png",
                    "group": "Ligue 1",
                    "forme": "DLWLL",
                    "description": "Promotion - Champions League (Group Stage)",
                    "all": {
                        "matchsPlayed": 35,
                        "win": 27,
                        "draw": 4,
                        "lose": 4,
                        "goalsFor": 98,
                        "goalsAgainst": 31
                    },
                    "home": {
                        "matchsPlayed": 18,
                        "win": 16,
                        "draw": 2,
                        "lose": 0,
                        "goalsFor": 59,
                        "goalsAgainst": 10
                    },
                    "away": {
                        "matchsPlayed": 17,
                        "win": 11,
                        "draw": 2,
                        "lose": 4,
                        "goalsFor": 39,
                        "goalsAgainst": 21
                    },
                    "goalsDiff": 67,
                    "points": 85,
                    "lastUpdate": "2019-05-04"
                },
                {...}
            ]
        ]
    }
}

And the HTML part to populate (however this would be step 2)以及要填充的 HTML 部分(但这将是第 2 步)

<div class="fifthRow">
        <div class="column">
          <div class="table" id="rank">
            <div><p></p></div>
            [...]
            <div><p></p></div>
          </div>

          <div class="table" id="logo">
            <div><p>Rank</p></div>
            <div><p></p></div>
            [...]
            <div><p></p></div>
          </div>

            [...]

在此处输入图像描述

On the face of it, that's an bit of an odd response format.从表面上看,这有点奇怪的响应格式。 The problem is that standings isn't the array of results, it's an array with a single array as its only element, and that array is the one with the results.问题是, standings不是结果数组,它是一个数组,只有一个数组作为它的唯一元素,而那个数组就是包含结果的数组。

If it's always an array with a single entry, then instead of:如果它始终是一个包含单个条目的数组,那么而不是:

var standings = response.api.standings;

you need你需要

var standings = response.api.standings[0];

But you'll want to review the documentation for the JSON feed you're consuming to understand why it has this extra layer, you may need a nested loop, eg:但是您需要查看您正在使用的 JSON 提要的文档,以了解为什么它有这个额外的层,您可能需要一个嵌套循环,例如:

for (const standing of response.api.standings) {
    for (const entry of standing) {
        console.log(entry.teamName);
    }
}

More about various ways to loop through arrays in my answer here .更多关于在我的回答中循环通过 arrays 的各种方法。

Problem is with var standings = response.api.standings;问题在于 var 排名 = response.api.standings;

actually standings is array of array.实际上排名是数组数组。

so access it like this所以像这样访问它

var standings = response.api.standings[0];

Small additional answer in relation to that response format which actually seems to be a nested array.与实际上似乎是嵌套数组的响应格式相关的附加小答案。 I handled to enter the array using response.data.api.standings[0][0]我使用response.data.api.standings[0][0]处理输入数组

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

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