Expand your javascript Array and javascript Array

Source: Internet
Author: User

Expand your javascript Array and javascript Array

The current project uses the jquery framework, Jquery miniui, Which is powerful, outstanding in performance, easy to use, and flexible. In the process of continuous learning and R & D, the miniui has inspired me a lot. I have re-understood the essence of js and realized the powerful functions of js.

Array Operations will inevitably be used when javascript is used. js functions become more and more powerful and widely used. The various data structures of many programming languages are essentially the same, they all encapsulate basic data types to form different functions, including distinctive objects, arrays, and collections. Next we will focus on js Array Operations.

First, let's talk about the js map. map is implemented by combining js arrays and objects in essence. It can achieve data access, query and deletion, and determine whether the object exists or not, since it is necessary to simulate writing, it should have all its functions.

For more information about Map, see the article http://blog.csdn.net/salerzhang/article/details/41116349.

The intersection and population of two Arrays:

/*** 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, 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 ();};

To determine whether an array contains an element, you can use contains or define a method by yourself to flexibly master the comparison method, similar to covering the equals method in the java class. For example:

Var objArr = [{name: "James", age: "24", sex: "male" },{ name: "James", age: "21", sex: "female" },{ name: "Zhang Yi", age: "23", sex: "male" },{ name: "Zhang Si", age: "25", sex: "female" },{ name: "zhangwu", age: "22", sex: "male"}]; Array. prototype. contain = function (obj, props) {var equals = true; for (var I = 0; I <this. length; I ++) {var thisObj = this [I]; for (var j = 0; j <props. length; j ++) {var prop = props [j]; equals = obj [prop] = thisO Bj [prop]? True: false; if (! Equals) {break ;}}if (equals) {break ;}} return equals ;}
How can we determine whether an object exists in an array? We have extended the contain method. The first parameter is the queried object, and the second parameter is the comparison content (attribute array), for example: when the object's name and age attributes are always required, they are determined to be equal:

Var obj = {name: "zhangwu", age: "22", sex: "male"}; var props = ["age", "name"]; alert (objArr. contain (obj, props ));

As you can see, alert returns true.

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.