Copy Code code as follows:
/**
* Each is a collection iteration function that takes a function as a parameter and a set of optional parameters
* This iterative function computes each element and optional parameter 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>
%}
* @param {function} fn for iterative judgment
* @param more ... 0 or more optional user-defined parameters
* @returns {Array} result set, if no result, return 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;
};
/**
* Get an array of elements that are not duplicated <br/>
* Uniqueness of an array
* @returns {array} consists of arrays of not repeating 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 {Array} two sets of complements
*/
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
* @returns {Array} intersection of two sets
*/
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 The difference set of two sets of {Array}
*/
Array.minus = function (A, b) {
Return A.uniquelize (). each (function (o) {return b.contains (o)? Null:o});
};
/**
* Find the 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 collection of two sets of {Array}
*/
Array.union = function (A, b) {
return A.concat (b). Uniquelize ();
};