The Identification Number and Password fields need to be filled in. Moreover, you cannot send messages devoid of contents Adding further data is not allowed because this topic has been closed Print You provided no password: you have to write it in the textfield beside the command delete for the topic you want to delete Are you sure you want to delete this comment? You did not cast a vote. You need to cast a vote by selecting a radio button. Impossible to proceed Provide the project number Insufficient form parameters: you forgot to fill in some field/s
 
member picmember pic
 
Snippets of A
 
info Below you can find the text of the snippet you want to read, and the list of the other snippets by this author if available.
What are snippets?
Share on MySpace

Javascript Compare Arrays Find Difference Or Shared Items

Snippet: Identification Number »   Snippet: Inclusion syntax »
Visitors: 8,658 Tagged by its author as: Programming Javascript Characters (in origin): 2,972 (pages: ~ 1)
Author: Em@il Permalink Cast your vote for this topic Printable version
OBJECTIVE: given two arrays, determine whether there are identical entries (that is, shared by both) or different entries (that is, unshared by both) and report such instances.  
function compareArrays(array1, array2, mode){
if(typeof array1!='object' || typeof array2!='object' || typeof array1['length']=='undefined' || typeof array2['length']=='undefined'){/*bad input*/return false;};
return (!mode)?
	array1.filter( function(i, arr){return array2.indexOf(i)>-1;} ).filter( function(i){return array2.filter( function(i){return array1.indexOf(i)>-1;} ).indexOf(i)>-1;} ):
[array1.filter( function(i, arr){return array2.indexOf(i)==-1;} ), array2.filter( function(i){return array1.indexOf(i)==-1;} )];
/*keep this comment to reuse freely
http://www.fullposter.com/?1*/}
Remove colors  
INPUTS: first two inputs must be two numerically indexed arrays.  
Last argument a number: if none or zero, the function returns the shared entries. If passed as number 1, the function returns the unshared entries.  
 
RETURNS: boolean false if the input arguments are not arrays.  
Otherwise if the third parameter is not passed or is passed as zero, it returns an array carrying the shared values.  
If the third parameter is passed as 1, it returns an array of two arrays, each of them again an array: the first array entry, placed at index 0, is an array that carries the values present in the first input array but not in the second input array; the second array entry, placed at index 1, is an array that carries the values present in the second input array but not in the first input array.  
 
CAVEATS: uses the built in javascript method filter, which is however not available on all platforms. Currently, all major browsers implement it, though.  
 
It is unknown how the function may perform on very big arrays. Being javascript, your function should run on reasonably long arrays namely fit to be handled on the client side (whose memory and calculation power is unknown nearly by definition), whereas for big input data it is advisable to perform such checks on the server side, possibly; for this purpose you may see PHP Array Compare Differences: Find In Arrays Items Identical, Added, Discarded or, also, Php To Find Identical Items Present In All The Input Arrays: Intersect Arrays, Php Search And Find Item In Arrays: Detect Presence In Any Or All Input Arrays  
 
Example of use:
var a1=[1,2,3,4,5,9];
var a2=[2,5,6,7,8];

var different=compareArrays(a1, a2, 1);
var equals=compareArrays(a1, a2, 0)

alert('array1 was: '+a1+'\narray2 was: '+a2+'\n\nPresent in array1 but not in array2: '+different[0]+'\nPresent in array2 but not in array1: '+different[1]+'\nPresent in both: '+equals);
Remove colors
Printable version To exclude hyperlinks from the print, check the checkbox »
Rss

Cast your vote for this topic

To perform this operation it is not necessary to be Full Poster members.
«Negative Positive»
Click here to save your vote (Current average: 0.00, Voters: 0)
Are you the author of this topic and do you want to append quickly more text? click here
Other topics
This subscriber has a blog too: Read the blog: 57 Shoutbox: 103 reviews: 153
Visitors: 8,658
Overall visits to all the topics: 4,974,131
Daily average (Calculated from the website subscription day): 2,063.95
Optional sorting commands:
Normal order: click here
Order by amount of visits: click here
Order by category: Programming Javascript
Current order: normal
Other categories available for this author (Limited data report: 100):
Advice: Martial Arts and Self Defense(4), Books(236), Critical Reviews and Essays(4), Dictionaries(1), Emergency Care(4), Epistles Letters and Advice(5), Fantasy Epics and Fables(1), History and Documents(5), Humor and Jokes(2), Methematics(2), Music and Lyrics(6), News Digests and Press Reviews(1), Novels Poetry and Stories(3), Philosophy Reviews(7), Poetry(1), Programming(4), Programming Ajax(2), Programming Javascript(82), Programming Php(52), Psychology(1), Quotes(1), Religion Esoterica and Spirituality(12), Scientifical Reviews(4), Self Improvement(1), Sport Activities and Apparels(2), Tarots(1)
Showing topics: 1, 10
Available total: 448
View only a list of the snippets by this author: click here.
Other topics available for this author: click on any title below to view the complete item:
Fighting Competently: Anticipation, And Remember It's In His Eyes Identification Number: 465 Visitors: 433 La Musa Segreta: Superiorità Onnipervasiva Della Boxe Identification Number: 464 Visitors: 1,211 Ha Senso Il Doping Sportivo? Effetto Matteo Nello Sport E Business Sportivo Identification Number: 463 Visitors: 1,457 The Musicians Within The Music Box And Other Hereafter Stories Identification Number: 462 Visitors: 2,712 Freud And Jung In A Nutshell: Three Or So Shots At Psychoanalysis For Dummies Identification Number: 461 Visitors: 4,172 Division The Math Of Gods: Ambiguities Of Antanairesis And New Math Operations Identification Number: 460 Visitors: 4,474 The Meaning Of Cruelty Identification Number: 459 Visitors: 4,851 Dÿanèra Ad Eleusi: La Folgorazione Ontologica: Il Pensare Sistematico E Non Identification Number: 458 Visitors: 5,622 Creative Writing: How To Write A Novel. Best Tips From The Bester Professionals Identification Number: 457 Visitors: 7,449 Newsreel Pseudo Intel: The Middle East Approaching The New 20s Identification Number: 456 Visitors: 7,422
External services
This page of this subscriber uses external services: Hide