简体   繁体   English

Javascript减少功能/三元运算符

[英]Javascript reduce function/ternary operator

function largestInEach(arr) {
    var resultArray = [],
        highestValue = 0;
    for (var i = 0; i < arr.length; i++) {
        highestValue = arr[i].reduce(function(a, b){
            return a >= b ? a : b;
        });
        resultArray.push(highestValue);
    } 
    return resultArray;
}

Can someone please explain this code. 有人可以解释一下此代码。

The rest of the code is very clear to me, but I have difficulty understanding the reduce function and its application. 其余代码对我来说很清楚,但是我很难理解reduce函数及其应用。

I agree with most of the other comments that you should search more and do self learning. 我同意其他大多数评论,您应该进行更多搜索并进行自我学习。 However, I know it is sometimes hard to find the exact info on how things work. 但是,我知道有时很难找到有关工作方式的确切信息。 So ill explain this to you. 病了,请向您解释。

Now coming to your code. 现在进入您的代码。

https://jsfiddle.net/Peripona/ppd4L9Lz/

It contains an array of arrays where you at the end create a resulted array with highest or lowest value elements from all the sub arrays. 它包含一个数组数组,最后您可以在其中创建一个包含所有子数组中最高或最低值元素的结果数组。

like you got 就像你有

var arry = [[1,2,3],[2,3,4],[20,-2,3]]

talking in layman terms... 用外行说话...

you got one array if you sort or reduce an array of integers, it might not always generate what you say for example if you got this data 如果对一个整数数组进行排序或归约,则会得到一个数组,例如,如果获得此数据,它可能不会总是生成您所说的内容

var ar = [1,3,34,11,0,13,7,17,-2,20,-21]

and if you do normal ar.sort() to get the sorted values 如果您执行普通的ar.sort()以获取排序后的值

you would expect something like this... as output " [-21, -2, 0, 1, 3, 7, 11, 13, 17, 20, 34] " 您会期望像这样...输出“ [-21,-2,0,1,3,7,11,11,13,20,34]”

but on the contrary you would get the output like this.. 但是相反,您将获得如下输出。

" [-2, -21, 0, 1, 11, 13, 17, 20, 3, 34, 7] "

Now you wonder... why this strange behavior and how does it matter anyhow to my Question.. 现在您想知道...为什么这种奇怪的行为及其对我的问题有何影响。

It Does matter.. 这很重要..

Cuz this is what you need to do to get the right output.. The way sort function is written has to work for for String and other types as well. 因为这是获得正确输出所需要执行的操作。编写排序函数的方法也必须适用于String和其他类型。 so they convert data into other formats when doing comparison on sort. 因此当进行排序比较时,它们会将数据转换为其他格式。

So all in all if you pass a function inside and specify that you need the in ascending order that is ab.. Descending order that is ba.. ar.sort(function(a,b){return ab;}) 因此,总而言之,如果您在内部传递一个函数并指定您需要ab的升序。.ba的降序.. ar.sort(function(a,b){return ab;})

Now coming to another part that is Reduce this function takes a function argument and get you the highest or the lowest value from the array. 现在进入另一个部分,Reduce该函数接受一个函数参数,并从数组中获取最大或最小的值。

therefore if you do.. 因此,如果您这样做。

ar.reduce(function(a,b){return a>=b ? b : a})

will give you -21 as the output.. 将为您提供-21作为输出。

and if you do 如果你这样做

ar.reduce(function(a,b){return a>=b ? a : b})

It will give you : 34 它会给你:34

So this function will take multidimensional arrays where each array contains some digits and this function will get you the highest from all those arrays.. 因此,此函数将采用多维数组,其中每个数组包含一些数字,并且此函数将使您从所有这些数组中获得最高收益。

I hope this Explains everything. 我希望这能解释一切。

Reduce function allows you to go through each item in an array, where you will be able to see previous array value, and current array value, in your case: Reduce函数允许您遍历数组中的每个项目,在这种情况下,您将可以查看先前的数组值和当前的数组值:

a = previous value, b = current value, a =先前值,b =当前值,

-(not in there)- -(不在那儿)-

i = index, currArray = the array you are working with. i =索引,currArray =您正在使用的数组。

and in your code you are comparing and returning if your previous value is greater than or equal to current value. 在您的代码中,如果您先前的值大于或等于当前值,则您正在比较并返回。

a >= b ? a : b;

Conditional (ternary) Operator which is (condition ? do this : or this ) -> Think of it like a if statement If(a >= b){ return a }else{ return b } 条件(三元)运算符,即(条件?执行此操作:或此操作)->将其视为if语句If(a> = b){返回} else {return b}

see Conditional (ternary) Operator 请参阅条件(三元)运算符

Also your 'arr' could be multi dimensional array. 同样,您的“ arr”可能是多维数组。 forexample Trying the following code on http://plnkr.co/edit/?p=preview 例如,在http://plnkr.co/edit/?p=preview上尝试以下代码

hit f12 for developer tools and look at console for results. 点击f12获取开发者工具,然后查看控制台获取结果。

var arr = [[1,2],[4,3],[5,23,52]];

var resultArray = [],
var highestValue;
for (var i = 0; i < arr.length; i++) {
    highestValue = arr[i].reduce(function(a, b){
        return a >= b ? a : b;
    });
    resultArray.push(highestValue);
} 

console.log(resultArray);

You result array contains [2, 4, 52]. 结果数组包含[2,4,52]。

I hope this helps. 我希望这有帮助。

JS reduce method is applied against two values of array and reduce these two values of array ( a and b) into one (c) based on defined condition (return c = a+b ). JS reduce方法适用于数组的两个值,并根据定义的条件(返回c = a + b)将数组的两个值(a和b)简化为一个(c)。 Here in your case the condition was which among two is greater (a>b?a:b). 在您的情况下,条件是两个之中哪个更大(a> b?a:b)。

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

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