简体   繁体   English

在字符串上实现 array.prototype.reduce()

[英]implement array.prototype.reduce() on string

I have some confusion regarding how reduce operation takes place on a string.First a new Str instance is created and sends desired string as a parameter.我对如何对字符串进行缩减操作感到困惑。首先创建一个新的 Str 实例并将所需的字符串作为参数发送。

Then using split method it splits into an array of string.A reduceIt method takes the array and execute a reduce operation which returns the array element which has height length.然后使用 split 方法将其拆分为一个字符串数组。reduceIt 方法获取该数组并执行 reduce 操作,该操作返回具有高度长度的数组元素。

It works fine with a two element array.But if there is more than two elements it returns NAN.它适用于两个元素的数组。但是如果有两个以上的元素,它返回 NAN。

Why it returns NAN for array having more than two elements??为什么它为具有两个以上元素的数组返回 NAN?

function Str(text){
   this.text=text.split('\n');
}
Str.prototype.reduceIt = function() {
  return this.text.reduce(function(first,last,index,arr) {
          return Math.max(first.length,last.length);

  });
};

var t=new Str('i am a boy\n she loves cats\n a goat ate my flower garden ');
console.log(t.reduceIt());

The first time the callback is called first is a string (the first element in the array), and your function makes sense when first and last are both strings, so it works when the callback is only called once (the array has at most 2 elements). 第一次调用回调first是一个字符串(数组中的第一个元素),以及你的函数是有道理的,当firstlast都是字符串,所以它的工作原理,当回调时才调用(数组至多2元素)。

The second time it is called it is the result of the previous call, a number . 第二次调用是上次调用的结果,即number When you call first.length on a number you get undefined and when you call Math.max on that you get NaN . 当您在数字上调用first.length ,您将得到undefined而当您在Math.max上调用时,您将得到NaN

If you want to find the length of the longest string in your array, you could use: 如果要查找数组中最长字符串的长度,可以使用:

Math.max.apply(Math, this.text.map(function (str) { return str.length; }));

Some good answers already. 已经有一些好的答案。 :-) :-)

The simple way to fix your problem is to supply an initial value of 0, then compare the returned value with the length of the new string, so: 解决问题的简单方法是提供一个初始值0,然后将返回值与新字符串的长度进行比较,因此:

Str.prototype.reduceIt = function() {
  return this.text.reduce(function(first,last,index,arr) {

          // Compare current value (first) with length of string
          return Math.max(first,last.length);

  }, 0); // supply 0 as the initial value
};

It might make things clearer to rename first to maxSoFar and last to currentString . 这可能会使事情更清晰重命名一个maxSoFar最后 currentString。

Why it returns NAN for array having more than two elements?? 为什么对于具有两个以上元素的数组返回NAN?

Because number.length is undefined , let's name your function foo and follow how it's invoked 因为number.lengthundefined ,所以让我们将函数命名为foo并遵循其调用方式

  1. foo(0, "i am a boy") gives NaN foo(0, "i am a boy")给出NaN
  2. foo(NaN, " she loves cats") gives NaN foo(NaN, " she loves cats")NaN
  3. foo(NaN, " a goat ate my flower garden ") gives NaN foo(NaN, " a goat ate my flower garden ")给出了NaN

Giving a final result of NaN . 给出NaN的最终结果。

This happens because number.length is undefined and Math.max(undefined, x) is NaN 发生这种情况是因为number.length不确定的,Math.max(undefined, x)NaN

It looks like you wanted to write a function which only takes the length of the second arg 看来您想编写一个仅占用第二个arg 长度的函数

function foo(a, b) {
    return Math.max(a, b.length);
}

In this case, you'll get 在这种情况下,您会得到

  1. foo(0, "i am a boy") gives 10 foo(0, "i am a boy")给出10
  2. foo(10, " she loves cats") gives 15 foo(10, " she loves cats")给出15
  3. foo(15, " a goat ate my flower garden ") gives 29 foo(15, " a goat ate my flower garden ")29

Giving a final result of 29 . 最终结果为29

Surprisingly reduce works natively with strings or any other array-like objects.令人惊讶的是, reduce可以原生地处理字符串或任何其他类似数组的对象。

Array.prototype.reduce.call('String payload', ...);

source 来源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM