Array definitions and operations in Javascript Jquery

Source: Internet
Author: User
Tags object object

Array definitions and operations in Javascript Jquery(2012-02-15 10:28:00) reproduced
Tags: gossip
1. Understanding Arrays

An array is a collection of types of data that can be integer, string, or even object
JavaScript does not support multidimensional arrays, but because arrays can contain objects (arrays are also an object), arrays can be nested in each other to implement functions like multidimensional arrays

1.1 Defining arrays

Declares an array with 10 elements

var a = new Array (10);

At this point A has opened up memory space, containing 10 elements, with the array name plus [subscript] to invoke, such as a[2] but at this time the element is not initialized, the call will return undefined

The following code defines a mutable array and assigns the value

var a = new Array ();
A[0] = 10;
A[1] = "AAA";
A[2] = 12.6;

As mentioned above, the array can put objects, such as the following code

var a = new Array ();
A[0] = true;
A[1] = document.getelementbyidx_x ("text");
A[2] = {x:11, y:22};
A[3] = new Array ();

Arrays can be assigned directly when instantiated, such as

var a = new Array (1, 2, 3, 4, 5);
var B = [1, 2, 3, 4, 5];

Both A and B are arrays, except that B uses the implicit declaration to create another instance, at which point the alert (A==B) will eject false

1.2 Multi-dimensional arrays

In fact, JavaScript does not support multidimensional arrays, in the ASP can use Dim a (10,3) to define a multidimensional array, in JavaScript, if the var a = new Array (10,3) will be an error
But as I said before, an array can contain objects, so you can declare an element inside an array as a group, for example

var a = new Array ();
A[0] = new Array ();
A[0][0] = 1;
Alert (a[0][0]); Pop up 1

Assigning values at the time of declaration

var a = new Array ([All-over], [4,5,6], [7,8,9]);
var B = [[+], [4,5,6], [7,8,9]];

As a result, a is a regular instantiation and B is an implicit declaration, resulting in a multidimensional array

1.3 Array Literals

This is not really know how to call Chinese, text array?
When it comes to arrays, we have to say that array literals, arrays are actually special objects, objects have unique properties and methods, through the object name. Property, Object. Method () to take the value and call, and the array is the subscript to take the value, arrays literals and arrays have a lot of similarities, is a collection of data types, but array literals is essentially an object, declaration, and invocation, which is different from the array.

var aa = new Object ();
aa.x = "Cat";
AA.Y = "Sunny";
alert (aa.x); Eject Cat

Create a Simple object, the general call is through aa.x, and if it is an array literals, the cat will pop up with alert (aa["X").

var a = {x: "Cat", Y: "Sunny"};
Alert (a["Y"]); Eject Sunny

This is another way to create an object, and the result is the same

2. Operation of array elements

As mentioned above, you can read and write elements by array [subscript].

The subscript range is 0– (23 (superscript 2)-1), when the subscript is negative, floating point, or even Boolean, the array is automatically converted to the object type, for example

var B = new Array ();
b[2.2] = "XXXXX";
Alert (b[2.2]); XXXXX

This is equivalent to b["2.2"] = "XXXXX"

2.1 Loop of the array

var a = [1,2,3,4,5,6];
for (var i =0; i<a.length; i++) {
Alert (A[i]);
}

This is the most commonly used, calendar array, the code will pop up 1 to 6

There is also a common

var a = [1,2,3,4,5,6];
for (Var e in a) {
Alert (e);
}

or pop 1 to 6,for...in is a calendar object (array is a special object) object, used on the array, because the array has no property name, so the direct output value, this structure statement used on the object, such as the following

var a = {X:1,y:2,z:3};
for (Var e in a) {
Alert (E + ":" + a[e]);
}

At this point e takes the property name, x, y, X, and to get the value, the array name [property], so a[e] is equivalent to a["X"], a["y"], a["Z"]

2.2 Common functions for arrays

Concat

Appends an array to an existing array and returns a new array without affecting the existing array

var a = [123];
var b = "Sunnycat";
var c = ["www", +, "Ido"];
var d = {x:3.14, y: "SK"};
var e = [1,2,3,4,[5,6,[7,8]];
Alert (A.concat (b)); 123,sunnycat
alert (a); 123
Alert (B.concat (c, D)); -Sunnycatwww,21,ido[object Object]
Alert (C.concat (b)); Www,21,ido,sunnycat
Alert (E.concat (11,22,33). Join ("#")); # 1 # 2 # 3 # 4 # 5,6,7,8 # 11 # 22 # 33

Note that you can only use the array or string, if the connection (the previous a) is a numeric value, a Boolean, an object, will be error, string concatenation array, the string will be concatenated with the first element of the array into a new element, and the array connection string will be appended to the new element (I do not know the reason, the person informed) For arrays that contain arrays and objects, the connection remains intact.

Join

Connect the array to a string using the specified spacer character.

var a = [' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' G '];
Lert (A.join (",")); A,b,c,d,e,f,g equivalent to A.tostring ()
Alert (A.join ("X")); -A x B x C x D x E x F x G

This is easy to understand, but it is important to note that only one-dimensional arrays are converted, and if there are arrays inside the array, they will not be followed by the string specified by the join, but rather by the default ToString (), for example

var a = [' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' G ', [11,22,33]];
Alert (A.join ("*")); A * b * c * d * e * f * g * 11,22,33

Arrays inside the array, and useless * connection

Pop

Deletes the last element of the array and returns the element

var a = ["AA", "BB", "CC"];
document.write (A.pop ()); cc
document.write (a); AA, BB

Returns undefined if the array is empty

Push

Adds an array after the array and returns the new length of the array

var a = ["AA", "BB", "CC"];
document.write (A.push ("DD")); 4
document.write (a); Aa,bb,cc,dd
document.write (A.push ([+])); 5
document.write (a); aa,bb,cc,dd,1,2,3

The difference with concat is that concat does not affect the original array, returns the new array directly, and push modifies the original array directly, returning the new length of the array

Sort

Array sorting, first look at an example

var a = [11,2,3,33445,5654,654, "ASD", "B"];
Alert (A.sort ()); 11,2,3,33445,5654,654,asd,b

The result is not very surprising, yes, the sort is not by the integer size, but the string comparison, is to take the first character of the ANSI code comparison, small row front, the same word to take the second character, if you want to compare by integer value, you can

var a = [11,2,3,33445,5654,654];
A.sort (function (b) {
return a-B;
});
alert (a); 2,3,11,654,5654,33445

The sort () method has an optional parameter, that is, the function in the code, this is a simple example, not the number of non-numeric sorting, not the number needs to make more judgments, here is not much to say

Reverse

Deserializes an array to compare with the first character ASCII value, as in sort ()

var a = [11,3,5,66,4];
Alert (A.reverse ()); 4,66,5,3,11

If the array also contains an array, then when the object is processed, the element will not be solved.

>var a = [' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' G ', [4,11,33]];
Alert (A.reverse ()); 4,11,33,g,f,e,d,c,b,a
Alert (A.join ("*")); 4,11,33 * g * f * e * d * c * b * a

It is supposed to be the last of the 11 rows, because here the 4,11,33 as a complete object comparison, so is ranked in the first place. If you don't see it, you can use the join () string to understand

Shift

Deletes the first element of the array and returns the element, similar to the pop

var a = ["AA", "BB", "CC"];
document.write (A.shift ()); -AA
document.write (a); Bb,cc

Returns undefined when the array is empty

Unshift

Instead of shift, add elements to the front of the array and return the new length of the array

var a = ["AA", "BB", "CC"];
document.write (A.unshift (11)); -4 Note: IE returns undefined
document.write (a); 11,aa,bb,cc
document.write (A.unshift ([11,22])); 5
document.write (a); 11,22,11,aa,bb,cc
document.write (A.unshift ("cat")); 6
document.write (a); Cat,11,22,11,aa,bb,cc

Note that the method, under IE will return undefined, seemingly Microsoft bug, I can correctly play the new length of the array in Firefox

Slice

Returning an array fragment

var a = [' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' G '];
alert (A.slice); b
Alert (A.slice (2)); C,d,e,f,g
Alert (A.slice (-4)); D,e,f,g
Alert (A.slice ( -2,-6)); -Space

A.slice, starting from subscript 1 to subscript 2, note that the element with the subscript 2 is not included
If there is only one argument, the default is to the end of the array
-4 is the 4th element of the countdown, so return the last four elements
The last line, starting from the bottom 2nd, is truncated, so it is clear that the preceding element is not taken, so an empty array is returned, and if you change to A.slice ( -6,-2) it returns b,c,d,e

Splice

Deletes an element of a fragment from an array and returns the deleted element

var a = [1,2,3,4,5,6,7,8,9];
document.write (A.splice (3,2)); 4,5
document.write (a); 1,2,3,6,7,8,9
document.write (A.splice (4)); -7,8,9 Note: The return empty under IE
document.write (a); 1,2,3,6
document.write (A.splice (0,1)); 1
document.write (a); 2,3,6
document.write (A.splice (1,1,["AA", "BB", "CC"])); 3
document.write (a); 2,aa,bb,cc,6,7,8,9
document.write (A.splice, "ee"). Join ("#"); Aa,bb,cc#6
document.write (a); 2,ee,7,8,9
document.write (A.splice, "cc", "AA", "tt"). Join ("#"); Ee#7
document.write (a); 2,cc,aa,tt,8,9

Note that the method under IE, the second parameter is required, not fill the default is 0, such as A.splice (4), in IE, the return empty, the effect is equivalent to A.splice (4,0)

Tostring

Convert an array to a string, not just an array, all objects can use this method

var a = [5,6,7,8,9,["A", "BB"],100];
document.write (A.tostring ()); 5,6,7,8,9,a,bb,100
var B = new Date ()
document.write (B.tostring ()); Sat 8 17:08:32 utc+0800 2009
var c = function (s) {
alert (s);
}
document.write (C.tostring ()); function (s) {alert (s);}

The Boolean value returns True or FALSE, and the object returns [object objectname]
Join () replaces only one-dimensional arrays compared to the join () method, and ToString () completely surfaces the entire array, regardless of one dimension or multidimensional
The method can also be used for 10-, 2-, 8-, and 16-input conversions, such as

var a = [5,6,7,8,9, "a", "BB", 100];
for (var i=0; i<a.length; i++) {
document.write (a[i].tostring () + "binary is" + a[i].tostring (2) + ", octal is" + a[i].tostring (8) + ", Hex is" + A[i].tos Tring (16)); 4,5
}

Output results

The binary of 5 is 101, octal is 5, Hex is 5
The binary of 6 is 110, octal is 6, Hex is 6
The binary of 7 is 111, octal is 7, Hex is 7
The binary of 8 is 1000, octal is 10, Hex is 8
The binary of 9 is 1001, octal is 11, Hex is 9
A's binary is a, octal is a, Hex is a
bb binary is bb, octal is bb, Hex is BB
The binary of 100 is 1100100, octal is 144, Hex is 64

The transform can only be performed on the element, and if the entire array is converted, the array is returned intact

toLocaleString

Returns the local format string, mainly used on the Date object

var a = new Date ();
document.write (A.tostring ()); Sat 8 17:28:36 utc+0800 2009
document.write (A.tolocalestring ()); August 8, 2009 17:28:36
document.write (A.tolocaledatestring ()); August 8, 2009

The difference is that toString () returns the standard format, tolocalestring () returns the local format full date (in Control Panel >> Regional and Language Options, by modifying [time] and [Long Date] format), tolocaledatestring () just like tolocalestring (), but less time.

ValueOf

Different primitive values are returned according to different objects, which is similar to ToString (), but ToString () returns the string type, while valueof () returns the original object type

var a = [1,2,3,[4,5,6,[7,8,9]];
var B = new Date ();
var C = true;
var d = function () {
Alert ("Sunnycat");
};
document.write (A.valueof ()); 1,2,3,4,5,6,7,8,9
document.write (typeof (A.valueof ())); Object
document.write (B.valueof ()); 1249874470052
document.write (typeof (B.valueof ())); Number
document.write (C.valueof ()); -True
document.write (typeof (C.valueof ())); , Boolean
document.write (D.valueof ()); function () {alert ("Sunnycat");}
document.write (typeof (D.valueof ())); function

The array is also an object, so typeof (A.valueof ()) Returns an object, returning a multidimensional array

var a = [1,2,3,[4,5,6,[7,8,9]];
var AA = a.valueof ();
document.write (aa[3][3][1]); 8

The Date object returns the number of milliseconds from January 1, 1970,
Math and Error objects do not have a valueof method

Array definitions and operations in Javascript Jquery

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.