我如何用NOT-1的答案来记录数组中正确元素的索引



我最多只能得到&quot-1〃;来自各种方法,如.indexOf、.findIndex、return、console.log。有一个单独的数组文件叫做fifaData.js,我可以检索一些信息,只是不能检索索引。我猜这是一个语法错误,但我已经做了好几天了,弄不明白。这是我有史以来的第一个问题,所以如果我很差劲,我很抱歉。我找到的其他答案都是"-1"。这绝对不是我想要的回报。

const popupQuery=prompt("Enter the Year , Data sought","1930,Home Team Name"), targetYear=popupQuery.split(",")[0],targetData=popupQuery.split(",")[1];
function fifaParse(targetYear){
for(let i=0; i<fifaData.length; i++) {
if (fifaData[i].year === targetYear && fifaData[i].Stage == "Group 4") {
console.log('i: ', i);}}}
fifaParse(targetYear)```
Example (fifa.js) :
```export const fifaData = [
{
"Year": 1930,
"Datetime": "13 Jul 1930 - 15:00",
"Stage": "Group 1",
"Stadium": "Pocitos",
"City": "Montevideo",
"Home Team Name": "France",
"Home Team Goals": 4,
"Away Team Goals": 1,
"Away Team Name": "Mexico",
"Win conditions": "",
"Attendance": 4444,
"Half-time Home Goals": 3,
"Half-time Away Goals": 0,
"Referee": "LOMBARDI Domingo (URU)",
"Assistant 1": "CRISTOPHE Henry (BEL)",
"Assistant 2": "REGO Gilberto (BRA)",
"RoundID": 201,
"MatchID": 1096,
"Home Team Initials": "FRA",
"Away Team Initials": "MEX"
},
{
"Year": 1930,
"Datetime": "13 Jul 1930 - 15:00",
"Stage": "Group 4",
"Stadium": "Parque Central",
"City": "Montevideo",
"Home Team Name": "USA",
"Home Team Goals": 3,
"Away Team Goals": 0,
"Away Team Name": "Belgium",
"Win conditions": "",
"Attendance": 18346,
"Half-time Home Goals": 2,
"Half-time Away Goals": 0,
"Referee": "MACIAS Jose (ARG)",
"Assistant 1": "MATEUCCI Francisco (URU)",
"Assistant 2": "WARNKEN Alberto (CHI)",
"RoundID": 201,
"MatchID": 1090,
"Home Team Initials": "USA",
"Away Team Initials": "BEL"
}]```

我觉得有数据类型问题

检查targetyearfifaData[i].year的类型

对于不考虑数据类型的匹配,可以将检查修改为使用===而不是==

if (fifaData[i].year == targetYear)

最新更新