JS takes two arrays of intersection | difference Set | set | complement | Redo Sample Code

Source: Internet
Author: User

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 inthis) {

    if(this[i] == needle) returntrue;

  }

  returnfalse;

}


-------------------------------------------------------------------------------------------
/**
* 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.