简体   繁体   中英

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. There is a separate array file called fifaData.js and I can retrieve some info, just not the index. 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'. 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

for matching not considering datatype you can modify the check to use == instead of ===

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM