P
ROTOTYPE
I'm Not Only The Prototype, I'm Also A Member.
home
▪
stats
▪
search
▪
linkback
▪
about
▪
FAQ
| user: guest,
login
,
register
Array
:
Array.crossRef()
author:
dark sylkro
[+]
,
Submitted: 12.28.01 5a
• Last Edit: 10.01.02 12p
Array.prototype.crossRef = function(arr1, arr2, func) { var arr1, arr2, func, common = new Array(); this.length = 0; for (var i=0; i<arr1.length; i++) { for (var j=0; j<arr2.length; j++) { if (arr1[i] == arr2[j]) { common.push(arr1[i]); } } } if (func or func == undefined) { for (var i=0; i<common.length; i++) { this.push(common[i]); } } else { var negObj = new Object(), whole = arr1.concat(arr2); // for (var i=0; i<whole.length; i++) { negObj[whole[i]] = true; } for (var i=0; i<common.length; i++) { negObj[common[i]] = false; } // for (var iter in negObj) { if (negObj[iter]) { this.push(iter); } } } return(this); }
usage
analyse 2 given arrays and either return an array of elements which they have in common, or an array of elements they don't have in common. the returned result is automatically palced in the third array and does not affect the original 2 arrays. Finally, it will not filter out duplicates, there's a method on this site that does that already if you're interested. So: array1 = new Array("Mon", "Tue", "Wed", "Thu"); array2 = new Array("Thu", "Fri", "Sat", "Sun"); // array3.crossRef(array1, array2, true); array3.crossRef(array1, array2); // // the above statements return common elements into array3: // "Thu" array3.crossRef(array1, array2, false); // // this however returns an array of the elements that aren't common // "Mon", "Tue", "Wed", "Fri", "Sat", "Sun" thanks to Gary Ma for finding a bug in the inverted function
msg
1
{
ifeelfree
[+]
, posted: 05.22.05 10p•-, top
[^]
}
nice one... just what I needed.. :)
Add Comment
[+]
›opyleft 2001-2010. Layer51 is: Jaime Prado.
@