简体   繁体   中英

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.

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

you would expect something like this... as output " [-21, -2, 0, 1, 3, 7, 11, 13, 17, 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. 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;})

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.

therefore if you do..

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

will give you -21 as the output..

and if you do

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

It will give you : 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:

a = previous value, b = current value,

-(not in there)-

i = index, currArray = the array you are working with.

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 }

see Conditional (ternary) Operator

Also your 'arr' could be multi dimensional array. forexample Trying the following code on http://plnkr.co/edit/?p=preview

hit f12 for developer tools and look at console for results.

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].

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 ). Here in your case the condition was which among two is greater (a>b?a:b).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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