Five ways to get a JavaScript array to go heavy _javascript tips

Source: Internet
Author: User
Tags array length javascript array

JavaScript Array to weight is a more common demand, there are many solutions, the Internet can find the answer, the following small series for everyone to organize a similar type of array to weight the method, first to introduce you to the simple realization of ideas.

Ideas:

Iterate over the array, compare it to the same, and delete the following

Iterate over the array, compare it to the same, skip the previous repeats, and add the new array

Take an array element into the new array, iterate through the remaining array elements, and compare the elements of the new array, if different, and put the new array.

Iterates through an array, takes an element, acts as an object's property, and determines whether the property exists

1. Delete the following duplicates:

function ov (arr) {
 //var a= (New Date). GetTime ()) for
 (Var i=;i<arr.length;i++) for
 (Var j=i+;j< arr.length;j++)
  if (Arr[i]===arr[j]) {Arr.splice (J,); j--;}  
 Console.info (New Date). GetTime ()-a) return  
 arr.sort (function (a,b) {return a-b});

2. This is the normal method, better understanding, if the same, jump out of the 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=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 where the J Loop went on, but the I value has changed. is equal to 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<ar.length;i++) {
 f=true;
 for (Var j=;j<m.length;j++)
 if (Ar[i]===m[j]) {f=false;break;};
 if (f) m.push (Ar[i])}
//console.info (new Date. GetTime ()-a) return 
 m.sort (function (a,b) {return a-b});
}

5. Use Object properties

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});
 

3 properties of a JavaScript array object

1, Length Property

The Length property represents the size of the array, which is the number of elements. Because the index of an array always starts with 0, the upper and lower bounds of an array are: 0 and length-1 respectively. Unlike most other languages, the length property of a JavaScript array is variable, which requires special attention. When the length property is set larger, the state of the entire array does not actually change, only the length property is larger, and when the length property is set to the previous hour, the value of the element whose index is greater than or equal to length in the original array is lost. The following is an example of changing the length property:

var arr=[12,23,5,3,25,98,76,54,56,76];

Defines an array that contains 10 digits

alert (arr.length); Displays the length of the array 10

arr.length=12; Increase the length of an array

alert (arr.length); The length of the display array has changed to 12

Alert (arr[8]); Displays the value of the 9th element, which is 56

arr.length=5; Reduces the length of the array to 5, and the elements indexed equal to or more than 5 are discarded

Alert (arr[8]); Show 9th element has changed to "undefined"

arr.length=10; Restores the array length to 10

Alert (arr[8]); Although the length is restored to 10, the 9th element cannot be retracted, displaying "undefined"

From the above code we can clearly see the nature of the length property. But the length object can be set not only explicitly, it may also be implicitly modified. You can use a variable that is not declared in JavaScript, and you can use an undefined array element (an element whose index exceeds or equal to length), at which point the value of the length property is set to the value plus 1 for the element index used. 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 first defines an array of 10 digits, which can be seen by an alert statement of 10. Then the element with index 15 is assigned to 15, or arr[15]=34, and then the length of the array is output by the alert statement, with 16. In any case, this is a surprising feature for developers who are accustomed to strongly typed programming. In fact, an array created with the new Array () has an initial length of 0, and it is an operation that does not define an element in it, which changes the length of the array.

As you can see from the above introduction, the length property is so magical that it makes it easy to increase or decrease the size of the array. Therefore, a thorough understanding of the length attribute is helpful to the flexible application in the development process.

2, prototype property

Returns a reference to the object type prototype. The prototype property is common to object.

Objectname.prototype

The objectname parameter is the name of the object.

Description: Provides a set of basic functions for an object's class with the prototype property. The operation of the new instance of the object, "inherit", gives the object a prototype.

For an array object, use the following example to illustrate the purpose of the prototype property.

Adds a method to the array object that returns the maximum element value in the array. To do this, declare a function, add it to the 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 executes, Y saves the maximum value in the array x, or says 6.

3, constructor property

Represents a function that creates an object.

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

Description: The constructor property is a member of all objects that have prototype. They include all of the JScript intrinsic objects except the Global and Math objects. The constructor property holds a reference to a function that constructs a particular object instance.

For example:

x = new String ("Hi");
if (X.constructor = = String)//To be processed (condition is true)

Or

function MyFunc {
//functions body.
}
y = new MyFunc;
if (Y.constructor = = MyFunc)//To be processed (condition is true)

The above is about the five methods of JavaScript array to be introduced and the three properties of JavaScript array object, I hope you like it.

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.