[英]How can i log the index of the correct element in array with an answer that is NOT -1
All i can get at best is "-1" from various methods like.indexOf, .findIndex, returning, console.logging.我最多只能从.indexOf、.findIndex、returning、console.logging等各种方法中得到“-1”。 There is a separate array file called fifaData.js and I can retrieve some info, just not the index.有一个名为 fifaData.js 的单独数组文件,我可以检索一些信息,而不是索引。 This is a syntactical error Im guessing but Ive been at this for days and cant figure it out.这是我猜测的语法错误,但我已经在这几天了,无法弄清楚。 Its my first question ever so Im sorry if I suck.这是我的第一个问题,如果我很糟糕,我很抱歉。 Other answers I've found have all resulted in '-1'.我发现的其他答案都导致'-1'。 This is definitely not what I want returned.这绝对不是我想要的回报。
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"
}]```
Looks like a datatype issue to me对我来说似乎是一个数据类型问题
Check the type of both targetyear and fifaData[i].year检查targetyear和fifaData[i].year的类型
for matching not considering datatype you can modify the check to use == instead of ===对于不考虑数据类型的匹配,您可以修改检查以使用==而不是===
if (fifaData[i].year == targetYear)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.