Home > Web Front-end > JS Tutorial > js takes the intersection of two arrays | difference set | union set | complement set | deduplication example code_javascript skills

js takes the intersection of two arrays | difference set | union set | complement set | deduplication example code_javascript skills

WBOY
Release: 2016-05-16 17:26:30
Original
1582 people have browsed it
复制代码 代码如下:

/**
* each is a set iteration function that accepts a function as a parameter and a set of optional parameters
* This iteration function sequentially calculates each element of the set and the optional parameters using the function, and calculates The result set returned is
{%example
<script> <br>var a = [1,2,3,4].each(function(x){return x > 2 ? x : null }); <br>var b = [1,2,3,4].each(function(x){return x < 0 ? x : null}); <BR>alert(a); <BR>alert (b); <BR></script>
%}
* @param {Function} fn Function for iterative determination
* @param more ... Zero or more optional User-defined parameters
* @returns {Array} result set, if there is no result, return an empty set
*/
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 a collection of non-repeating elements in an array

* Unique an array
* @returns {Array} An array composed of non-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 the complement of two sets
{%example
<script> <br>var a = [1,2,3,4]; <br>var b = [3,4 ,5,6]; <br>alert(Array.complement(a,b)); <br></script>
%}
* @param {Array} a set A
* @param {Array} b Set B
* @returns {Array} The complement of the 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> <br>var a = [1,2,3,4]; <br>var b = [3,4, 5,6]; <br>alert(Array.intersect(a,b)); <br></script>
%}
* @param {Array} a set A
* @ param {Array} b Set B
* @returns {Array} The intersection of two sets
*/
Array.intersect = function(a, b){
return a.uniquelize().each(function(o){return b.contains(o) ? o : null});
};

/**
* Find the difference between two sets
{%example
<script> <br>var a = [1,2,3,4]; <br>var b = [3,4 ,5,6]; <br>alert(Array.minus(a,b)); <br></script>
%}
* @param {Array} a set A
* @param {Array} b Set B
* @returns {Array} The difference between the two sets
*/
Array.minus = function(a, b){
return a.uniquelize().each(function(o){return b.contains(o) ? null : o});
};

/**
* Find the union of two sets
{%example
<script> <br>var a = [1,2,3,4]; <br>var b = [3,4 ,5,6]; <br>alert(Array.union(a,b)); <br></script>
%}
* @param {Array} a set A
* @param {Array} b Set B
* @returns {Array} The union of two sets
*/
Array.union = function(a, b){
return a.concat(b).uniquelize();
};
Related labels:
js
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template