JS take the intersection of two arrays | difference Set | set | complement | Go heavy sample code _javascript tips

Source: Internet
Author: User
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 ();
};
Related Article

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.