Five methods for removing JavaScript arrays _ javascript tips-js tutorial

Source: Internet
Author: User
Tags javascript array
There are many methods to de-duplicate javascript arrays. You can find the answer everywhere on the Internet. The following section provides you with a method to de-duplicate arrays of the same type, if you are interested, check it out. Deduplication of javascript ArraysIt is a common requirement and there are many solutions. You can find answers online. The following small series will help you sort out a method for deduplication of arrays of the same type, let's introduce some simple implementation ideas.

Ideas:

Traverse the array, compare them one by one, and delete

Traverse the array, compare them one by one, compare them to the same one, skip the previous duplicate, and put the different into the new array

Take an array element and add it to the new array. traverse the remaining array elements and compare them with the elements of the new array. If there are different elements, add them to the new array.

Traverse the array and take an element as the object attribute to determine whether the attribute exists.

1. Delete the following duplicate:

Function ov (arr) {// var a = (new Date). getTime () for (var I =; I

2. This is a conventional method and is easy to understand. If the same method is used, the loop jumps out.

function ov(a) { //var a=((new Date).getTime()) var b = [], n = a.length, i, j; for (i = ; i < n; i++) { for (j = i + ; j < n; j++)  if (a[i] === a[j]){j=false;break;} if(j)b.push(a[i]); } //console.info((new Date).getTime()-a)  return b.sort(function(a,b){return a-b});}

3. It took me a long time to understand this. Although the j loop continues here, the I value has changed. It is like a new I loop:

function ov(a) { //var a=((new Date).getTime()) var b = [], n = a.length, i, j; for (i = ; i < n; i++) { for (j = i + ; j < n; j++) if (a[i] === a[j])j=++i b.push(a[i]);} //console.info((new Date).getTime()-a)  return b.sort(function(a,b){return a-b});}

4. Ensure that the new array is unique.

Function ov (ar) {// var a = (new Date). getTime () var m = [], f; for (var I =; I

5. Use object attributes

function ov(ar){// var a=(new Date).getTime() var m,n=[],o= {}; for (var i=;(m= ar[i])!==undefined;i++) if (!o[m]){n.push(m);o[m]=true;}// console.info((new Date).getTime()-a)  return n.sort(function(a,b){return a-b});; }

Three attributes of the javascript array object

1. length attribute

The Length attribute indicates the Length of the array, that is, the number of elements. Because the index of an array always starts from 0, the upper and lower limits of an array are: 0 and length-1. Unlike most other languages, the length attribute of the JavaScript array is variable, which requires special attention. When the length attribute is set to a greater value, the status of the entire array does not actually change, except that the length attribute becomes larger. When the length attribute is set to an hour later than the original value, all the values of the elements whose indexes are greater than or equal to the length in the original array are lost. The following is an example of changing the length attribute:

Var arr = [,];

// Defines an array containing 10 numbers

Alert (arr. length); // display the length of the array by 10

Arr. length = 12; // increase the length of the array.

Alert (arr. length); // display that the length of the array has changed to 12

Alert (arr [8]); // displays the value of the 9th elements, 56

Arr. length = 5; // reduce the length of the array to 5. elements whose index is equal to or greater than 5 are discarded.

Alert (arr [8]); // display that 9th elements have changed to "undefined"

Arr. length = 10; // restore the array length to 10

Alert (arr [8]); // although the length is restored to 10, 9th elements cannot be recovered, and "undefined" is displayed"

From the code above, we can clearly see the nature of the length attribute. However, the length object can be explicitly set and may be implicitly modified. JavaScript can use an undeclared variable, or an undefined array element (an element whose index exceeds or is equal to length, the value of the length attribute is set to the value of the used element index plus 1. For example, the following code:

var arr=[12,23,5,3,25,98,76,54,56,76];alert(arr.length);arr[15]=34;alert(arr.length);

The Code also defines an array containing 10 numbers. The alert statement shows that the length is 10. Then, an element with an index of 15 is used and assigned a value of 15, that is, arr [15] = 34. Then, the array length is output using the alert statement, and the result is 16. In any case, this is a surprising feature for developers who are used to strong-type programming. In fact, the initial length of an Array created in the form of new Array () is 0, and the length of the Array changes only when no element is defined.

From the above introduction, we can see that the length attribute is so magical that it can be used to conveniently increase or decrease the array capacity. Therefore, an in-depth understanding of the length attribute can be used flexibly in the development process.

2. prototype attributes

Returns a reference to an object type prototype. The prototype attribute is common to objects.

ObjectName. prototype

The objectName parameter is the name of the object.

Note: The prototype attribute is used to provide a set of basic functions of the object class. The new instance of the object "inherits" the operation that is granted to the object prototype.

The following example describes the purpose of the prototype attribute for array objects.

Add a method to the array object to return the maximum element value in the array. To do this, declare a function, add it to Array. prototype, and use it.

function array_max( ){ var i, max = this[0]; for (i = 1; i < this.length; i++) { if (max < this[i]) max = this[i]; } return max;}Array.prototype.max = array_max;var x = new Array(1, 2, 3, 4, 5, 6);var y = x.max( );

After the code is executed, y saves the maximum value in array x, or 6.

3. constructor attributes

The function that creates an object.

Object. constructor // object is the name of an object or function.

Note: The constructor attribute is a member of all objects with prototype. They include all inherent JScript objects except Global and Math objects. The constructor attribute stores references to the functions used to construct a specific object instance.

For example:

X = new String ("Hi"); if (x. constructor = String) // process (the condition is true)

Or

Function MyFunc {// function body .} Y = new MyFunc; if (y. constructor = MyFunc) // process (the condition is true)

The above content is about the five methods for de-duplicating JavaScript arrays and the three attributes of javascript array objects described in this article.

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.