簡體   English   中英

當我嘗試調用我的函數時,為什么會出現“未定義”?

[英]Why am I getting an 'undefined' when I try to call my function?

我正在嘗試遍歷一個包含多個數組的數組。 數組看起來像這樣。 var numsArr = [ [1, 2, 3, 4], [5, 6], [7, 8, 9, 10, 11]];

我已經嘗試做一個基本的 for 循環(I = 0; I < numsArr.length; I++) 當我嘗試返回numsArr[I]我得到了所有數組,但在所有數組返回之后,最后我也得到了一個“未定義”。

這就是我的代碼。

var numsArr = [ [1, 2, 3, 4], [5, 6], [7, 8, 9, 10, 11]];

function looper(){
  for(let i = 0; i < numsArr.length; i++){
    console.log(numsArr[i])
  }
}
console.log(looper())

我希望結果是數組 numsArr 中的每個數組,它確實返回每個數組,但最后有一個“未定義”。

[ 1, 2, 3, 4 ]
[ 5, 6 ]
[ 7, 8, 9, 10, 11 ]
undefined

 var numsArr = [ [1, 2, 3, 4], [5, 6], [7, 8, 9, 10, 11]]; function looper(){ for(let i = 0; i < numsArr.length; i++){ console.log(numsArr[i]) } } looper()

您不需要console.log() looper() ,因為它已經記錄了結果。

只需在您的 JS 底部編寫looper() ,它應該可以正常工作。

您的函數需要返回 undefined 以外的值以顯示在 console.log 中

暫無
暫無

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

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