【问题标题】:Iteration over an Array of Arrays in vuejs在 vuejs 中迭代数组数组
【发布时间】:2020-02-05 14:45:45
【问题描述】:

在这个 JSON 示例中是否有一种最简单的方法来遍历数组的数组


  "game": {

  "sunk_ships": [
    [
      "PatrolBoat",
      "Destroyer"
    ]
  ]
}

在我的代码中,这是我考虑到以前回答的问题而写的

for (let i = 0; i < this.getGamePlayerId.sunk_ships.length; i++) {
          let childArray=parent[i];

          for (let j = 0; j < parent[i].length; j++) {
            var innerValue=parent[i][j]
            console.log(parent[i][j]);

              document
                .getElementById(innerValue)
                .classList.add("shipDestroyed");
          }
        }

谢谢

【问题讨论】:

标签: javascript arrays


【解决方案1】:

JSON 是文本数据交换格式。

你要写的是一个嵌套循环,有很多方法可以写。

const json = `{
    "game": {

        "sunk_ships": [["PatrolBoat", "Destroyer"], ["Frigate", "Carrier"]]
    }
}`;

const o = JSON.parse(json)

o.game.sunk_ships.forEach((ships) => 
                          ships.forEach((ship) => 
                              console.log(ship)))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多