Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • JavaScript - Learn how to find the difference in multiple arrays using reduce

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2
    • 0
    • 508
    Comment on it

    So when we talk about iteration on arrays in JavaScript, first thing comes in mind is traditional loops, but here we are discussing something that might surprise you that is array reduce() method, so first I’ll tell you, how does array reduce() method works?

     

     

    The array reduce()  method reduces the array of values to a single value, it takes an array and executes a given function once for each item in the array (from left-to-right).

     

    Syntax:  array. reduce (function(total, currentValue, currentIndex ), initialValue);

                    total (required)previously returned or initial value.
                    currrentValue ( required) :
    value of current element.
                    currentIndex (optional)
    : index of current element in array.
                    initialValue (optional)

     

    Example:

    var arr = [1,2,3,4,5,6];
    
    arr.reduce (function ( total, current, index ){
    
    return total+current
    
    }, 0);

     

    Note: If we don’t pass the initial value, the callback won’t iterate over the first element of the array. Instead, the total was set to the value of the first element.

     

    What if we have multiple arrays, and want to do something like finding symmetric difference in arrays then how would you do that? Loop? No way, So, reduce can help you with that:

    function findDiff() {
    
    	var arrays = [].slice.call( arguments ); // convert arguments to array of arrays
    	var firstarr = arrays[ 0 ];		// fetch the first array from arrays
    	var restarr = arrays.slice( 1 );		// all remaining arrays from arrays
    return firstarr.reduce( function ( prev, curr ) {
    
    	var push = restarr.every( function ( subArray ) {
         //The every() method tests whether all elements in the array pass the test implemented by the provided function.
    	return subArray.indexOf( curr ) > - 1;  //indexOf() returns the position of the first occurrence of a specified value in a string.
    
    } );
    	if ( push ) {
    
    	prev.push( curr );
    
    }
    	return prev;
    
    }, [] );
    
    }
    findDiff( [ .2, 1, 5, 32 ], [ 32, 20, 5, .2, 24 ], [ 5, .2, 23, 32, 82 ] ) ;
    

     

    I hope you find the article helpful, For a depth overview read Mozila docs for Array.prototype.reduce and for any questions or feedback please feel free to write in comment section below.

    JavaScript - Learn how to find the difference in multiple arrays using reduce

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: