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
• 253
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 ){

}, 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.

## 0 Comment(s)

OR
OR
Register

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

• Hire
• Post Projects

### Post Projects

• All at 0 Cost ....
• Post Tech Job
• Select Best Bidder
• Track the Project
• Approve Work and Pay safely
• Browse Nerds
• Work
• Find Projects Find Projects
• UI Design and UX
• Software Engineering
View more...
View less...
• Marketing
• General
• Manage
• Company Company

### Manage Company

• All at 0 Cost ....
• Manage Company and Employee Profiles
• Company wide Employee Productivity Reports
• Knowledge Sharing and Collaboration Tools
• Get Sales Lead and Bid for Tech Projects
• Send Invoices and Receive Payment Safely
• Learn
• Nerd Digest Nerd Digest
• UI Design and UX
• Software Engineering
View more...
View less...
• Marketing
• General
• Tech Q & A Tech Q & A
• UI Design and UX
• Software Engineering
View more...
View less...
• Marketing
• General