javascript - 在 vuejs 中迭代数组数组
问题描述
在这个 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");
}
}
谢谢
解决方案
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)))
推荐阅读
- html - 视频高度最大到屏幕高度
- c# - 在 Datagrid 中显示 Excel 中的数据
- javascript - 如何在反应中的CSelect元素中显示从api获取的数组
- c++ - 洗牌向量的最快“简单”方式
- android - 设置意图在 SAMSUMG M51 中不起作用
- r - 将数据框 Group BY 代码放入 R 中的函数中
- virtualbox - VirtualBox 6.1 在运行几分钟后自动重启我的主机
- redis - Vertx 4 - Redis 6 问题
- javascript - 当用户单击另一个链接(如分步菜单)时,如何添加另一个检查类以替换当前活动类?
- javascript - 我想检索反应表排序的数据并将其传递给后端是否可能。因为我是新手,所以我想知道我们该怎么做