Icontem

JavaScript Array Compare: Compare arrays to check for common values

Recommend this page to a friend!
  Info   View files Documentation   View files View files (2)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2016-02-19 (3 years ago) RSS 2.0 feedNot enough user ratingsTotal: 83 All time: 441 This week: 6Up
Version License JavaScript version Categories
array-compare 1Free To Use But R...5Data types, Algorithms, Math
Description Author

This package can compare arrays to check for common values.

It adds new prototype functions to the array object, so it can compare with another array object in different ways. Currently it can:

- Check if there are any positions in the array where both arrays have the same value
- Look for any values that are in both arrays regardless of their positions
- Find matches between the arrays regardless of the position but include the same value multiple times if it occurs that many times in both arrays

Innovation Award
JavaScript Programming Innovation award winner
February 2016
Winner
The JavaScript Array object can implement many standard features but it does not implement all array comparing functionality that sometimes you need.

This package implements additional JavaScript Array functions for comparing arrays in several ways like matching, intersecting and overlapping.

Manuel Lemos
  Performance   Level  
Name: Stephen Chapman <contact>
Classes: 11 packages by
Country: Australia Australia
Age: ???
All time rank: 181 in Australia Australia
Week rank: 18 Equal1 in Australia Australia Equal
Innovation award
Innovation award
Nominee: 4x

Winner: 1x

Details

Three methods to provide ways for comparing two arrays

matches

This method compares two arrays to see if there are any positions in the array where both arrays have the same value. It returns the values where matches are found.

var m = a.matches(b);

intersect

This method compares the two arrays looking for any values that are in both arrays regardless of their positions. It only returns one instance of each value that is in both arrays regardless of the number of times that value occurs.

var i = a.intersect(b);

overlap

This method also finds matches between the arrays regardless of the position but unlike intersect the overlap method will include the same value multiple times IF it occurs that many times in BOTH arrays.

var o = a.overlap(b);

Usage

In each case you call it on one array and pass the second array as a parameter. In each case both of the original arrays remain unaffected by the call. Each call returns its results as a new array (which can be empty if the arrays don't have anything that meets the criteria).

Additional Info

Both matches and overlay return an array with values of the same type as the inputs and return the values in the order they appear in the array the method is called on.

intersect converts all the values in the returned array to strings as a part of the process of removing any duplication. It also returns the values is ascending sort order.

All three methods use an identically equal compare and so only return those values that have both the same type and value in both arrays.

In each of the code examples shown both a and b are arrays. The methods will not work if you try to pass something other than an array as b.

Each method relies on other array methods introduced in ES5 in 2009 so if you still have lots of visitors using antiquated browsers you may need polyfills for filter and forEach.

  Files folder image Files  
File Role Description
Plain text file arraycomp.js Class Compare Methods
Accessible without login Plain text file instructions.txt Doc. Instructions

 Version Control Unique User Downloads Download Rankings  
 0%
Total:83
This week:0
All time:441
This week:6Up