我有2个冠军的阵列。

每个冠军都有自己的球队和竞争对手

我应该如何选择具有我需要的Championship_id的数组元素。

我的意思是,我将需要访问{{team.name}},在PHP中,我具有ChampionshipId的值,这意味着在代码中的任何地方,我都可以包含:championship-id= 13

我不知道该怎么做是选择具有Championship_id = 13的数组

还是应该更改阵列结构,以便更轻松地获得所需的团队和竞争对手?

这是我的数组的结构

   array:2 [▼
0 => array:3 [▼
  "championship" => 13
  "competitors" => array:1 [▼
    0 => array:2 [▼
      "id" => 104
      "name" => "[email protected]"
    ]
  ]
  "teams" => array:3 [▼
    0 => array:2 [▼
      "id" => 1
      "name" => "t1"
    ]
    1 => array:2 [▼
      "id" => 2
      "name" => "t2"
    ]
    2 => array:2 [▼
      "id" => 3
      "name" => "t3"
    ]
  ]
]
1 => array:3 [▼
  "championship" => 15
  "competitors" => array:5 [▼
    0 => array:2 [▼
      "id" => 104
      "name" => "[email protected]"
    ]
    1 => array:2 [▼
      "id" => 105
      "name" => "julien2"
    ]
    2 => array:2 [▼
      "id" => 106
      "name" => "[email protected]"
    ]
    3 => array:2 [▼
      "id" => 107
      "name" => "[email protected]"
    ]
    4 => array:2 [▼
      "id" => 108
      "name" => "[email protected]"
    ]
  ]
  "teams" => array:2 [▼
    0 => array:2 [▼
      "id" => 4
      "name" => "rr1"
    ]
    1 => array:2 [▼
      "id" => 5
      "name" => "rr2"
    ]
  ]
]


]

最佳答案

Array.prototype.find可能正是您想要的。


  find()方法返回满足提供的测试功能的数组中第一个元素的值。否则返回undefined。


所以:

const arr = ...
const targetID = 13;
const championship = arr.find((elem) => elem.championship_id == targetID);

关于javascript - 选择其中Championship_id = 13的数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42799447/

10-14 04:33