[英]Why does Array.map() method returns arr[index+1].length as undefined?
使用Array.map()方法時出現一個奇怪的錯誤。 為什么arrayElementLength = strarr [index + 1] .length拋出一個錯誤,它是未定義的(即使它可以工作)
function longestConsec(strarr, k) { // your code let solution = []; strarr.map((string, index, arr) => { let arrayElementLength = strarr[index+1].length; console.log(arrayElementLength); if(string.length == arrayElementLength){ solution.push(string); } }); return solution; } console.log(longestConsec(["zone", "abigail", "theta", "form", "libe", "zas"], 2))
當您到達strArr
的最后一個索引而不是index+1
您正在嘗試訪問數組中不存在的索引,這是導致此錯誤strarr[(index + 1)] is undefined
function longestConsec(strarr, k) { // your code let solution=[]; strarr.map((string,index,arr)=>{ let arrayElementLength=strarr[index+1].length; console.log(arrayElementLength); if(string.length==arrayElementLength){ solution.push(string); } }) return solution; } console.log(longestConsec(["zone", "abigail", "theta", "form", "libe", "zas"], 2))
附帶說明:您沒有使用map的返回值,所以最好使用forEach
為什么arrayElementLength = strarr [index + 1] .length拋出一個錯誤,它是未定義的(即使它可以工作)
不,它在上一次迭代中不起作用。 數組索引從0
開始。 使用strarr[index+1]
,在上一次迭代中,您的代碼嘗試從實際上不存在的索引訪問該項目,並返回undefined
。
var sampleArr = ['test']; console.log(sampleArr[0]); //test console.log(sampleArr[1]); //undefined
因此,從索引部分刪除+1
,您的代碼將按預期工作:
function longestConsec(strarr, k) { // your code let solution=[]; strarr.map((string,index,arr)=>{ let arrayElementLength=strarr[index].length; console.log(arrayElementLength); if(string.length==arrayElementLength){ solution.push(string); } }) return solution; } console.log(longestConsec(["zone", "abigail", "theta", "form", "libe", "zas"], 2))
這並不奇怪,數組索引從0開始,因此最后一個索引+ 1將是不確定的。
此外, Array.map
通常用於將給定的數組“映射”到新數組,您使用它的方式更適合使用Array.forEach
。
function longestConsec(strarr, k) {
let solution = [];
strarr.forEach((string, index, arr) => {
let arrayElementLength = strarr[index].length;
console.log(arrayElementLength);
if(string.length === arrayElementLength){
solution.push(string);
}
})
return solution;
}
console.log(longestConsec(["zone", "abigail", "theta", "form", "libe", "zas"], 2))
正如其他人已經提到的那樣,當循環到達最后一個元素時,您會收到錯誤消息。 strarr[index+1]
返回undefined
,您不能訪問它的length
。
查看您的代碼,如果您打算獲取與下一個元素具有相同length
所有項目,則filter
適合您的目的:
function longestConsec(strarr, k) { return strarr.filter((string, index) => { return index !== strarr.length - 1 && string.length === strarr[index + 1].length }) } console.log(longestConsec(["zone", "abigail", "theta", "form", "libe", "zas"], 2))
我添加了一個附加的檢查index !== strarr.length - 1
如果循環遇到最后一個元素, index !== strarr.length - 1
。
Array.map
遍歷映射中的每個元素,並且index
(第二個參數)是當前索引,現在,如果到達要映射的數組中的最后一個元素,則下一個元素( index + 1
)根本不存在。
如果整個函數都試圖獲得相同長度length的最長條紋,則可能只想reduce
基本數組。
function longestConsec(arr) { return arr .reduce((carry, string) => { const prev = carry[carry.length - 1]; const length = string.length; if (!prev || prev.length !== string.length) { carry.push({ length, strings: [string] }); } else { prev.strings.push(string); } return carry; }, []) .sort((one, two) => two.strings.length - one.strings.length) .shift().strings; } console.log(longestConsec(["zone", "abigail", "theta", "form", "libe", "zas"]))
(請注意,我從函數中刪除了k
參數,因為似乎沒有使用它)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.