map函数不返回数组字的长度

时间:2017-10-10 14:55:57

标签: javascript

[初学者的任务],我有一个String,我通过split()变成了一个数组,然后我学会了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");

8 个答案:

答案 0 :(得分:1)

map返回将指定函数应用于列表中的每个元素作为另一个列表的结果。在您的情况下,您将获得列表中每个元素的长度,因此应用map:

[3, 5, 5, 3, 6, 4, 3, 4, 3]

如果你想获得最长的数字,你只需要迭代它并选择它

答案 1 :(得分:1)

传递给.map()方法的函数由JavaScript运行时自动传递3个参数(您不需要做任何事情来实现这一点):

  • 对要枚举的数组项的引用
  • 要枚举的数组项的索引
  • 对要枚举的数组的引用

与所有JavaScript参数一样,您可以选择显式接收一些,不使用或全部,只需在回调函数中设置命名参数(您可以将参数调用为您喜欢的任何有效标识符名称)。

现在,对于您的任务, .map 确实不是最佳选择, Array.reduce() 是(因为它只返回一个值) ),但你可以让.map()工作,你只需 get the highest number out of the resulting array

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"));

答案 2 :(得分:0)

.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的值为.length。然后,每次迭代使用Math.max()将先前值与当前单词的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);,并返回最大的成员。

让它更简洁

最后一个解决方案建立在现有代码的基础上,但可以通过避免变量赋值和使用箭头函数来缩短它。

Firestore

答案 3 :(得分:0)

您可以这样使用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"));

答案 4 :(得分:0)

最简单且在我看来最正确的方法是将字句边界上的句子分开,然后映射长度,只需使用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") )

答案 5 :(得分:0)

你已经在分裂后返回所有单词的长度数组。您需要做的就是找到最大值。 在代码中添加一行就可以了:

    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;

答案 6 :(得分:0)

也可以使用排序:

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];
}

如果您还希望同时找到最小值,则会很有趣。

答案 7 :(得分:0)

最后我使用了以下内容:

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");

我知道这不是最干净或更简洁的,但我不是先进或知识渊博的使用箭头功能。谢谢或者所有的帮助。请随时回答我如何缩短这个特定的解决方案,以便我可以解决它。干杯!

相关问题