以嵌套循环结尾

时间:2019-03-20 00:58:34

标签: javascript reactjs

我正在通过React / Redux / Laravel应用进行某种游戏管理,自从该项目开始以来,我就遇到了问题。

由于我有来自API的嵌套结构,因此我面临嵌套的for循环。

例如,我有这种事情:

for (let typeItems of items) {
        for (let item of typeItems) {
            for (let typeCharac of characs) {
                for (let charac of typeCharacs.characs) {
                    for (let stats of item.stats) {

                        //apply stats

                    }
                }
            }
        }
    }

对于每种类型的物品,我都必须寻找每种物品。对于每种类型的特征,我必须检查每种特征,最后检查该项目的每个统计信息,如果特征ID符合该项目的统计信息ID,我将应用它们。

这在我的文件中更加复杂。有避免这种事情的技巧吗?我很迷路

谢谢!

编辑:对于前两个for循环,我有这样的东西:

{
  "items": [
    {
      left:[
      "id_item" : 1,
      "stats": [
        {
          "id_stats": 1
        },
        {
          "id_stats": 2
        },
        {
          "id_stats": 3
        }
      ]
    }
  ],
  right:[
          "id_item" : 2,
      "stats": [
        {
          "id_stats": 1
        },
        {
          "id_stats": 4
        },
        {
          "id_stats": 6
        }
      ]
    ]
  ]
}

然后在其他两个循环中这样:

[
  {
    "id_typechar": 1,
    "charac": [
      {
        "id_stats": 1
      },
      {
        "id_stats": 2,
      },
      {
        "id_stats": 4,
      }
      ]
  },
    {
    "id_typechar": 2,
    "charac": [
      {
        "id_stats": 6
      },
      {
        "id_stats": 8,
      },
      {
        "id_stats": 3,
      }
      ]
  },

]

0 个答案:

没有答案