Http://www.jb51.net/article/40385.htm
The code is as follows:
/**
* Each is a set iteration function that takes a function as a parameter and a set of optional parameters
* This iteration function computes each element and optional argument of the collection in turn, and returns the computed result set
{%example
<script>
var a = [1,2,3,4].each (function (x) {return x > 2? x:null});
var b = [1,2,3,4].each (function (x) {return x < 0? x:null});
alert (a);
alert (b);
</script>
%}
* function to iterate over @param {function} fn
* @param more ... 0 or more optional user-defined parameters
* @returns {Array} result set, if there is no result, returns empty
*/
Array.prototype.each = function (fn) {
fn = fn | | FUNCTION.K;
var a = [];
var args = Array.prototype.slice.call (arguments, 1);
For (var i = 0; i < this.length; i++) {
var res = fn.apply (This,[this[i],i].concat (args));
if (res! = NULL) A.push (res);
}
return A;
};
--------------------------------------------------------------------------------------------
/**
* Array.prototype.[method name] allows you to define/overwrite an objects method
* needle is the item you are searching for
* this is a special variable that refers to "this" instance of an Array.
* returns true if needle is in the array, and false otherwise
*/
Array.prototype.contains =
function
( needle ) {
for
(i
in
this
) {
if
(
this
[i] == needle)
return
true
;
}
return
false
;
}
-------------------------------------------------------------------------------------------
/**
* Get an array of elements that do not duplicate the collection <br/>
* Uniqueness of an array
* @returns {array} arrays consisting of distinct elements
*/
Array.prototype.uniquelize = function () {
var ra = new Array ();
For (var i = 0; i < this.length; i + +) {
if (!ra.contains (This[i])) {
Ra.push (This[i]);
}
}
return RA;
};
----------------------------------------------------------------------------
/**
* Find a complement of two sets
{%example
<script>
var a = [1,2,3,4];
var b = [3,4,5,6];
Alert (Array.complement (A, b));
</script>
%}
* @param {Array} A collection a
* @param {Array} b Collection B
* @returns The complement of {Array} two sets
*/
array.complement = function (A, b) {
return Array.minus (Array.union (A, B), Array.intersect (A, b));
};
-------------------------------------------------------------------------------------------
/**
* Find the intersection of two sets
{%example
<script>
var a = [1,2,3,4];
var b = [3,4,5,6];
Alert (Array.intersect (A, b));
</script>
%}
* @param {Array} A collection a
* @param {Array} b Collection B
* Intersection of two sets @returns {Array}
*/
Array.intersect = function (A, b) {
return A.uniquelize (). each (function (o) {return b.contains (o)? O:null});
};
-----------------------------------------------------------------------------------
/**
* Find the difference set of two sets
{%example
<script>
var a = [1,2,3,4];
var b = [3,4,5,6];
Alert (Array.minus (A, b));
</script>
%}
* @param {Array} A collection a
* @param {Array} b Collection B
* @returns {Array} difference set of two sets
*/
Array.minus = function (A, b) {
return A.uniquelize (). each (function (o) {return b.contains (o)? Null:o});
};
----------------------------------------------------------------------------------
/**
* Find a set of two sets
{%example
<script>
var a = [1,2,3,4];
var b = [3,4,5,6];
Alert (Array.union (A, b));
</script>
%}
* @param {Array} A collection a
* @param {Array} b Collection B
* @returns The set of {Array} two collections
*/
array.union = function (A, b) {
return A.concat (b). Uniquelize ();
};
JS takes two arrays of intersection | difference Set | set | complement | Redo Sample Code