Array knowledge points in private custom JavaScript

Source: Internet
Author: User
Tags filter foreach anonymous array length arrays sort try catch javascript array

First joke, 1. Just saw a swimming, think of company organization to Sanya Tourism, the boss jumped the sea, all kinds of struggle, fished up the boss first sentence: I remember I can swim AH.

2. Daughter-in-law said: Husband sorry, I put your new bike crashed into pieces! Husband: Nothing baby, if you are well, it is sunny! Daughter-in-law said: "Husband you are too poetic." Hubby: Roll bastard, I'll kill you!

The concept of an array

JavaScript arrays are worthy of an ordered collection, but it is a special form of a JavaScript object, which is a very important qualitative.

Create an array

1.var a=new Array ();//equal to []

2.var a=new Array (10); An array of specified lengths

3.var a=new Array (1,2, "Marry You");

4.var a=[];

The array is dynamic

First, the array can be accessed by [] to access the array elements, which can be read and written to the array, where [] the array will automatically maintain its length when the property name is less than 232 nonnegative integers. The first is a clear distinction between the index of the array and the property name of the object, all indexes are property names, but only the integer property name between 0~232-2 is the index.

All arrays are objects, and you can create properties of arbitrary names for array objects. However, if you use a property that is an index of an array, the special behavior of the array is more necessary to update its Length property value.

This corrects the concept of a JavaScript array "Out of Bounds" error, and if an attempt is made to access the array index beyond its length, no error is made and only the undefined value is returned.

Sparse arrays and dense arrays

The concept is an array of values that contain discontinuous indexes starting at 0. It can also be understood that the length property value is greater than the number of elements. Then this will be more efficient than the normal dense array in memory, but the search efficiency is basically the same.

You can now use in to detect

var a1=[,,,];

0 in A1//false

Addition and deletion of array elements

The simplest method of deletion is Var a=[1,2,4]; a.length=0;

You can also use Object.defineproperty () to make the array length property read-only

var a=[1,2,4];  
Object.defineproperty (A, "length", {writable:false});  
a.length=0;  
Console.log (a);  
[1, 2, 4]

Add with push (1,3,4,5,7)//Here you can add multiple elements at once

Delete A[1] does not modify the length property of array A, it is equivalent to the deletion of the position of the element undefined value, then the modified array becomes a sparse array. So you should use POP () This function will return the deleted element

Array traversal

Traversal array with for/in or for. The former traversal is not in a certain order, so it can be replaced with foreach (The new function in ECMAScript 5) or for, while traversal is the best cache array length for example

for (Var i=0,alength=a.length;i++) {  
//loop body  
} for  
      
(var i in a) {  
if (!a.hasownproperty (i)) {  
Continue Jump out of circulation body  
//Cycle Body  
}  
}

Methods of arrays

Commonly used methods are join ()/reverse ()/sort ()/concat ()/slice ()/splice ()/push () ()/pop ()-/unshift ()/shift ()/tostring ()

If you know all of the above, then I would like to say one of the ways

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/script/

If you use sort without sorting it is sorted by character, what if you have a number or a sort of processed character?

Sort () This method can pass an anonymous function as an argument, and the anonymous function can satisfy some of our special requirements.

A=[' A ', "bug", "good", "CCC"];  
A.sort (function (s,t) {  
var a=s.tolowercase ();  
var b=t.tolowercase ();  
if (a<b) return-1;  
if (a>b) return 1;   
return 0;   
});       
["A", "Bug", "CCC", "good"]

New Array method in ECMAScript 5

ForEach ()//traversal of the array, in order output, if you want to break, you need to use a try catch to assist.

Map ()//a=[1,2,3]; A.map (function (x) {return x*x;})//[1,4,9];

Filter ()//a=[5,3,4,5,6]; A.filter (function (x,i) {return i%2==0});//[4,6]

Every ()/some () ()/reduce ()/reducerigth ()/indexof ()/lastindexof () and so on if you don't know, brother Gu.

The point here is that the fact that the string behaves like an array makes it possible for a common array method to be applied to a string.

For example:

Array.prototype.filter.call ("JavaScript", function (x) {return x.match (/[^aeiou]/);}). Join ("");  
      
"Jvscrpt"

Conclusion

The simple conclusion is that the nature of many things that the array shows is actually determined by the object. Most of the methods in an array are generic, which means that it is not only valid for array objects, but also for "class arrays objects" such as string behavior similar to character arrays. If you think it's good, then recommend 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.