[英]map function doesn't return length of array's words
[初學者的任務],我有一個String,它通過split()
變成了一個數組,然后我了解了map(function(x))
,該map(function(x))
應該通過一個函數返回另一個數組。
目的是確定並return
數組中最長的單詞作為數字,但是運行我的代碼時,我只會得到所有單詞的數字。 我知道完成任務缺少一些東西,但是我想知道a)為什么map
的函數采用一個參數(x),而該參數表示原始數組中的每個單詞? b)我想完成什么任務? 我不確定哪種算法只會顯示最大的#。 在此先感謝您,對不起您的解釋。
function findLongestWord(str) { var array = str.split(' '); var lengths = array.map(function(x){ return x.length; }); return lengths; } findLongestWord("The quick brown fox jumped over the lazy dog");
map返回將指定函數應用到列表中的每個元素作為另一個列表的結果。 在您的情況下,您將獲得列表中每個元素的長度,因此應用map將獲得:
[3, 5, 5, 3, 6, 4, 3, 4, 3]
如果要獲得最長的數字,只需迭代並選擇它
傳遞給.map()
方法的函數由JavaScript運行時自動傳遞3個參數(您無需做任何事情即可實現):
與所有JavaScript參數一樣,您可以選擇簡單地通過在回調函數中設置命名參數來顯式地接收其中的任何一個(無)或全部接收(您可以將其命名為任意有效的標識符名稱)。
現在,對於您的任務, .map
確實不是最佳選擇, Array.reduce()
是(因為它僅返回單個值),但是您可以使.map()
起作用,您只需要獲取結果數組中最高的數字 :
function findLongestWord(str) { var array = str.split(' '); // The callback function will automatically be passed 3 parameters. // Use them or not depending on what you are doing in the callback. var lengths = array.map(function(item, index, arry){ console.log(item, index, arry); // Just for demonstration return item.length; // Put the current array item into the resulting array }); return lengths; // Return the resulting array to the caller } // You'll need to call the Math.max() method and pass // it the array to get the highest number in the array console.log(Math.max.apply(null, findLongestWord("The quick brown fox jumped over the lazy dog")));
為此,請使用.reduce()
方法:
function findLongestWord(str) { // Take the string and split into an array of words that then // has the replace() method applied to those words: return str.split(' ').reduce(function(a, b) { // We want the length of the longer word returned // but, after the first iteration, the parameter "a" // won't be a word, it will be the number returned // from the last iteration, so we need to do Math.max // against either the word's length (first iteration) // or the value of a returned from the prior iteration // on the second and subsequent iterations a.length will // be undefined so just a will be used. return Math.max(a.length || a, b.length); }); } console.log(findLongestWord("The quick brown fox jumped over the lazy dog"));
.map()
獲取其參數的位置 a)為什么map的函數采用一個參數(x),並且該參數表示原始數組中的每個單詞?
這就是.map()
的實現方式的工作方式。 它迭代該數組的所有成員,並在每次迭代中調用一次回調,傳入當前數組成員,並接收返回的所有內容,並將其放入正在構建的新數組中。
.reduce()
和Math.max()
完成任務 b)我錯過了什么來完成任務? 我不確定哪種算法只會顯示最大的#。
我不會為此使用.map()
。 您可以改用.reduce()
。
function findLongestWord(str) { return str.split(' ').reduce((mx, word) => Math.max(mx, word.length), 0); } var result = findLongestWord("The quick brown fox jumped over the lazy dog"); console.log(result);
.reduce()
方法的工作方式有所不同。 每次對回調的調用都具有上一次迭代( mx
)和當前單詞( word
)的返回結果。 提供了0
值作為初始值,因此它是第一次迭代時的mx
值。 然后,每次迭代都會使用Math.max()
將先前的值與當前單詞的.length
進行比較,后者會返回更大的值,從而使其在下一次迭代時成為mx
的值。
最后,最后一次迭代返回的值成為最終結果。
.map()
數組與Math.max()
和“擴展語法”一起使用 但是,如果要使用從.map()
創建的數組,則還可以使用擴展語法將其內容傳遞給Math.max()
。
function findLongestWord(str) { var array = str.split(' '); var lengths = array.map(function(x) { return x.length; }); return Math.max(...lengths);; } var result = findLongestWord("The quick brown fox jumped over the lazy dog"); console.log(result);
這會將所有lengths
的成員作為單獨的參數傳遞給Math.max
,並返回最大的成員。
最后一個基於現有代碼的解決方案,但是可以通過避免變量分配和使用箭頭功能來將其縮短一點。
function findLongestWord(str) { return Math.max(...str.split(' ').map(x => x.length)); } var result = findLongestWord("The quick brown fox jumped over the lazy dog"); console.log(result);
您可以使用reduce
這種方式,因為您只需要一個輸出
function findLongestWord(str) { var array = str.split(' '); return array.reduce(function(memo, x){ return memo > x.length ? memo : x.length; }, -1); } console.log(findLongestWord("The quick brown fox jumped over the lazy dog"));
在我看來,最簡單,最正確的方法是在單詞邊界上分割句子,然后映射長度,然后簡單地使用Math.max
返回最大數
function findLongestWord(str) { return Math.max.apply(null, str.split(/\\b/).map( x=>x.length) ); } console.log( findLongestWord("The quick brown fox jumped over the lazy dog") )
拆分后,您已經返回了所有單詞的長度數組。 您需要做的就是找到最大的價值。 在代碼中添加一行即可使其正常工作:
function findLongestWord(str) {
var array = str.split(' ');
var lengths = array.map(function(x){
return x.length;
});
//---------------------------------------------
return Math.max.apply( Math, lengths );
//This is what you're missing to complete your task
}
var result = findLongestWord("The quick brown fox jumped over the lazy dog");
console.log(result);
typeof result;
也可以使用排序:
function findLongestWord(str) {
var array = str.split(' ');
var res = array.map(function (x, i) {
return {length: x.length, pos: i};
}).sort(function (a1, a2) {
return a2.length - a1.length
})[0];
return array[res.pos];
}
如果您還希望同時找到最小值,則很有趣。
最后,我將其與以下各項一起使用:
function findLongestWord(str) { var arrr = str.split(' '); var lengths = arrr.map(function(individualarrayword){ return individualarrayword.length; }); return lengths.reduce(function(a, b) { return Math.max(a, b); }); } findLongestWord("The quick brown fox jumped over the lazy dog");
我知道這不是最干凈或更簡潔的方法,但是我對使用箭頭功能並不了解。 謝謝或所有幫助。 請隨時回答我如何縮短此特定解決方案,以便我可以解決它。 干杯!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.