簡體   English   中英

JavaScript 在函數之間傳遞變量以定位數組中的 object

[英]JavaScript passing variables between functions to target an object in an array

(警告)第一次發帖,盡我所能。 如果格式錯誤,請提前道歉。

我正在按照以下說明創建一個簡單的加密貨幣轉換器:

*定義一個名為“addCurrency”的function,它需要3個arguments:一個“硬幣”一個“值”和一個數組(“加密”)

在里面使用 Array.prototype.findIndex 來查看 'coin' 是否已經存在於 'crypt' 數組中。 如果找到索引,則存儲它。

如果不是:將其推送到 'crypt' 數組。 然后返回一個字符串,如下所示,將單詞 COIN 替換為您添加的“硬幣”的名稱(大寫)。

如果存在,請調用“findCurrency”以獲取匯率。

定義一個名為“findCurrency”的 function,它需要 3 個 arguments:一個“硬幣”、一個“價值”、一個索引(來自“addCurrency”)和“crypt”數組。 使用索引從“crypt”數組中訪問正確的“coin”來獲取“coin”的匯率。

調用轉換器 function 傳遞“價值”、匯率和“硬幣”。 定義一個 function 稱為轉換器,它需要 3 個 arguments、一個“價值”、一個匯率和一個“硬幣”。

執行轉換並返回調用 tellConversion 到 output 的結果。 定義一個名為 tellConversion 的 function,它需要 3 個 arguments 一個結果,一個“硬幣”和一個“價值”。

使用 tellConversion 的 arguments 創建示例中所示的字符串。*

在控制台中運行下面的代碼,我得到:

未捕獲的類型錯誤:無法讀取未定義的屬性“速率”

在這一行 --> var coinRate = crypt[coinIndex].rate;

coinIndex 注銷到控制台,crypt 在初始 function 調用中傳遞。 為什么 var coinRate 未定義?

我錯過了什么?

var addCurrency = (coinObj, value, crypt) => {
    
    function findCurrency(crypt, coinIndex, value){
-->     var coinRate = crypt[coinIndex].rate;
        console.log(coinRate)
        converter(value, coinRate)
    }
            
    function converter(value, coinRate){
      var converted = coinRate * value;
        console.log(converted)
        tellConversion(value, converted, coinObj)
    }
    function tellConversion(value, converted, coinObj){
      return `You will receive ${converted} usd for your ${value} ${coinObj.coin}`
    }

    var coinIndex = crypt.findIndex(obj => obj.coin === coinObj.coin)
        console.log(coinIndex)
        if(coinIndex === -1){
            crypt.push(coinObj)
            console.log(crypt)
            return `New coin ${coinObj.coin.charAt(0).toUpperCase() + coinObj.coin.slice(1)} added to Database`
            
        } else { findCurrency(value, coinIndex, coinObj) }
        
  }

調用 function:

var crypt = [{coin:'eth', rate:800}]
addCurrency({coin:'eth', rate:800}, 2, crypt)

傳遞參數的順序不正確, findCurrency function。

更新findCurrency function 以僅接受coinIndex作為參數。

function findCurrency(coinIndex){
  var coinRate = crypt[coinIndex].rate;
  console.log(coinRate)
  converter(value, coinRate)
}

valuecrypt變量將在findCurrency中可用,因為它們在同一個 scope 中。

總體代碼:

var addCurrency = (coinObj, value, crypt) => {
    
  function findCurrency(coinIndex){

     var coinRate = crypt[coinIndex].rate;
      console.log(coinRate)
      converter(value, coinRate)
  }
          
  function converter(value, coinRate){
    var converted = coinRate * value;
      console.log(converted)
      tellConversion(value, converted, coinObj)
  }
  function tellConversion(value, converted, coinObj){
    return `You will receive ${converted} usd for your ${value} ${coinObj.coin}`
  }

  var coinIndex = crypt.findIndex(obj => obj.coin === coinObj.coin)
      console.log(coinIndex)
      if(coinIndex === -1){
          crypt.push(coinObj)
          console.log(crypt)
          return `New coin ${coinObj.coin.charAt(0).toUpperCase() + coinObj.coin.slice(1)} added to Database`
          
      } else { findCurrency(coinIndex) }
      
}

var crypt = [{coin:'eth', rate:800}]
addCurrency({coin:'eth', rate:800}, 2, crypt)

暫無
暫無

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

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