简体   繁体   English

JavaScript 使用三元运算符减少

[英]JavaScript reduce with ternary operator

I have a piece of code that just works.我有一段可以正常工作的代码。 This code looks for the largest value in the array of numbers.此代码查找数字数组中的最大值。 Could someone translate this to simple JavaScript(without ternary) so a novice programmer can understand it?有人可以将它翻译成简单的 JavaScript(没有三元),以便新手程序员可以理解它吗?

  const mostVotes = votes.reduce((bestIndex, v, i, arr) => v > arr[bestIndex] ? i : bestIndex, 0);

At first, I was trying to implement Math.max, but I needed the index of the largest value in the array, so I went after reduce and this is what I was trying to do with it.起初,我试图实现 Math.max,但我需要数组中最大值的索引,所以我采用了 reduce,这就是我试图用它来做的。

const mostVotes = votes.reduce((acc, value, i, arr) => {
  if(value > acc) {
    return i
  }
}, 0)

Thanks for the answers, much appreciated!感谢您的回答,非常感谢! I'm starting to understand this and it's much clearer now.我开始明白这一点,现在更清楚了。 Javascript reduce and ternary together is a nice fit. Javascript reduce 和三元结合在一起是一个很好的选择。

Essentially, the code you provided is looping through each element in votes and checking whether it is greater than an element stored at a particular index.本质上,您提供的代码是循环遍历votes每个元素并检查它是否大于存储在特定索引处的元素。 This index is stored in the variable bestIndex an is used to mark/keep track of the index which holds the largest element from all elements seen while looping.该索引存储在变量bestIndex ,用于标记/跟踪索引,该索引包含循环时看到的所有元素中的最大元素。

In your example, your ternary is checking if a given element is larger than the currently marked biggest element (by doing v > arr[bestIndex] ).在您的示例中,您的三元正在检查给定元素是否大于当前标记的最大元素(通过执行v > arr[bestIndex] )。 If this is the case we then set the index of the current element to be the new position of the largest element (by implicitly returning i ).如果是这种情况,我们将当前元素的索引设置为最大元素的新位置(通过隐式返回i )。 If this is not the case, we leave the index of the largest element as it is by implicitly returning bestIndex .如果不是这种情况,我们通过隐式返回bestIndex保留最大元素的索引。

You can translate this into a more procedural style of programming by using for loops and if-statements like so:您可以通过使用 for 循环和 if 语句将其转换为更程序化的编程风格,如下所示:

 let votes = [-4, 10, 100, -3, 40]; let positionOfMax = 0; for(let i = 0; i < votes.length; i++) { if(votes[i] > votes[positionOfMax]) { // v > arr[bestIndex] positionOfMax = i; // ? i (from ternary) } /* Not needed else {posittionOfMax = positionOfMax} // : bestIndex (from ternary) */ } console.log(positionOfMax);

I encourage you to take a look at .reduce() and the documentation on the conditional (ternary) operator .我鼓励您查看.reduce()和有关条件(三元)运算符的文档。 They're both useful and powerful tools which can help speed up your development.它们都是有用且强大的工具,可以帮助您加快开发速度。

What might be confusing about the original code was the lack of brackets {} .原始代码可能令人困惑的是缺少括号{}

() => 'test' is the same as () => { return 'test' } () => 'test'() => { return 'test' }

In your case:在你的情况下:

(bestIndex, v, i, arr) => v > arr[bestIndex] ? i : bestIndex

(bestIndex, v, i, arr) => {
  return (v > arr[bestIndex] ? i : bestIndex)
}

(bestIndex, v, i, arr) => {
  if(v > arr[bestIndex])
    return i
  else 
    return bestIndex
}

const mostVotes = votes.reduce((bestIndex, v, i, arr) => {
  if(v > arr[bestIndex])
    return i
  else 
    return bestIndex
}, 0);

The below if/else should get you to where you want to be.下面的if/else应该能让你到达你想去的地方。

if (v > arr[bestIndex]) {
  return i
} else {
  return bestIndex
}

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

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