Js operations Array Function instance summary _ javascript skills

Source: Internet
Author: User
This article mainly introduces js operations on Array functions. The example shows how JavaScript can delete specified elements, deduplication, and delete specified subscript elements from arrays, for more information about js operations, see the following example. We will share this with you for your reference. The details are as follows:

1. Delete the specified element from the array.

/*** Reference instance foreach = function (obj, insp) {if (obj = null & obj. constructor! = Array) {return [];} // obj is the Array to be processed. obj = null indicates that the object does not exist. obj. constructor! = Array indicates that the constructor of the object obj attribute is not an Array; // The constructor attribute always points to the constructor of the current object. If both conditions are met, an empty array [] is returned. // The following describes the constructor attributes. Var obj = [1, 2, 3, 4]; // equivalent to var obj = new Array (1, 2, 3, 4); console. log (obj. constructor = Array); // returns true, indicating that the obj constructor is Array; var foo = function () {}; // equivalent to var foo = new Function (); console. log (foo. constructor = Function); // return true to indicate that the foo constructor is Function; var obj = new Foo (); // The constructor instantiates an obj object console. log (obj. constructor = Foo); // return true, indicating that the obj constructor is Foo; * // Delete the specified function del (val, arr) in the array {// check Test the parameter if (arr = null & arr. constructor! = Array) {return [];} var newarr = []; // save nonexistent values to the new Array for (var I = 0; I <arr. length; I ++) {if (arr [I]! = Val) newarr. push (arr [I]);} return newarr;} alert (del (2, [1, 2, 3, 4, 5, 2]);

2. Remove duplicate elements

/*** Remove repeated elements from the array and save the element value as the key of a new array. The key cannot be repeated, then you can set the number of variables */function unique (data) {data = data | []; var a ={}; len = data. length; for (var I = 0; I <len; I ++) {var v = data [I]; if (typeof (a [v]) = 'undefined') {a [v] = 1 ;}}; data. length = 0; for (var I in a) {data [data. length] = I;} return data;} alert (unique ([12, 12, 12, 34]);

3. Delete the element of the specified base object in the array.

/*** Delete the specified subscript element of the array ** the I value is always changing, and the n value is changed only when the if condition is true (it will increase sequentially) */Array. prototype. remove = function (dx) {if (isNaN (dx) | dx> this. length) {return false;} for (var I = 0, n = 0; I
 
  

I hope this article will help you design JavaScript programs.

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.