簡體   English   中英

我如何記錄數組中正確元素的索引,答案不是 -1

[英]How can i log the index of the correct element in array with an answer that is NOT -1

我最多只能從.indexOf、.findIndex、returning、console.logging等各種方法中得到“-1”。 有一個名為 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM