Collection of common JavaScript Array tools

Source: Internet
Author: User
/*** Each is a set iteration function that accepts a function as a parameter and a set of optional parameters * This iteration function calculates each element and optional parameter of the set using a function in sequence, return the calculated result set {% example <SCRIPT> var A = [1, 2, 4]. each (function (x) {return x> 2? X: NULL}); var B = [1, 2, 4]. Each (function (x) {return x <0? X: NULL}); alert (a); alert (B ); </SCRIPT> %} * @ Param {function} FN indicates the function for iterative determination * @ Param more... zero or multiple optional User-Defined parameters * @ returns {array} result set. If no result exists, empty Set */array is returned. 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). push (RES) ;}return ;}; /*** get an array of elements that are not repeated <br/> * uniquely define an array * @ returns {array} an array composed of Non-repeated elements */array. prototype. uniqustme = function () {var Ra = new array (); For (VAR I = 0; I <this. length; I ++) {If (! RA. contains (this [I]) {Ra. push (this [I]) ;}} return Ra ;};/*** calculate the complement set of the Two sets {% example <SCRIPT> var A = [1, 2, 4]; vaR B = [3, 4, 5, 6]; alert (array. complement (a, B )); </SCRIPT> %} * @ Param {array} a set a * @ Param {array} B Set B * @ returns {array} complement set */array. complement = function (a, B) {return array. minus (array. union (a, B), array. intersect (a, B) ;};/*** calculates the intersection of the 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. uniqustme (). each (function (o) {return B. contains (o )? O: NULL}) ;};/*** calculate the difference set {% example <SCRIPT> var A = [1, 2, 3, 4] of the Two sets; var B = [3, 4, 5, 6]; alert (array. minus (a, B )); </SCRIPT> %} * @ Param {array} a set a * @ Param {array} B Set B * @ returns {array} The difference set of the Two sets */array. minus = function (a, B) {return. uniqustme (). each (function (o) {return B. contains (o )? Null: O}) ;};/*** calculate the union of the 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 {array} the union of the two sets */array. union = function (a, B) {return. concat (B ). uniqustme ();};

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.