JavaScript Note 4-arrays

Source: Internet
Author: User

I. Overview:
1. Arrays are untyped: each element of the same array can be any type, or it can be an array or an object;
2. Index starting from 0, maximum to 2^32-2=4294967294; accommodating up to 4,294,967,295 elements;
3. The array is dynamic and automatically increases or decreases as needed; no need to declare size when creating an array, and no need to reallocate space when size changes;
4. Arrays can be contiguous (non-sparse arrays) or discontinuous (sparse arrays), and for sparse arrays, length is larger than the index of all elements;
5. The implementation of the array is usually optimized, and it is much faster to access the object with a numeric index than to access the regular object properties.

Two. Creating an array
1.var empty = []; An empty array named empty
2.var primes = [1,2,3,4]; Array of 4 elements
3.var Misc = [1.1, True, "a",];//contains 3 types and a comma array
4.
var base = 1;
var table = [Base, base+1, base+2, base+3];//can contain an expression
5.var B = [[1,{x:1,y:2}],[2,{x:3,y:4}]];//contains the direct amount of the object or other array
6.var count = [1, 3];//2nd element is undefined
7.var defs = [, the];//array contains 2 elements, all of which are undefined, the syntax of the array's direct amount allows for an optional trailing comma, so the array has only 2 elements;
8.var a = new Array ();//Call the array () function, without parameters, equivalent to creating an empty array;
9.var a = new Array (10);//Call the array () function to create a function of length 10;
10.var a = new Array (5,4,3, "testing, testing");//parameter is the element of the new array, i.e. there are 4 elements, 3 integers + 1 strings;

Three. addition/deletion of array elements
1. Assign a value to the new index, which can be added, such as a[2]=2;
The 2.push () method is added at the end of the array, such as A.push (2), which is consistent with A[a.length]
The 3.unshift () method is added to the header of the array, and the other elements move back
4.delete Delete array elements, the array length is unchanged when deleted, the position no longer has elements, non-sparse arrays become sparse arrays
such as: a = [a]
Delete A[1];
After deleting an element with an index of 1, a.length=3; Console.log (a[1]) returns undefined, the array becomes a sparse array

Four. Array traversal
1. General for Loop
var a =[1,2,3, NULL,,];
Delete A[1];
var len = a.length;
Console.log (len);//5
for (var i = 0; i<a.length; i++) {
Skipping null/undefined and non-existent elements
if (!a[i])
Continue
Console.log (A[i]);//1 3
Skipping undefined and non-existent elements
if (a[i]===undefined)
Continue
Console.log (A[i]);//1 3 null
Skipping elements that do not exist
if (! ( I in a))
Continue
Console.log (A[i]);//1 3 null
}
2.forEach
The sum of the squares of the elements of the following array
var a =[1,2,3, NULL,,];
Delete A[1];
var sq=0;
A.foreach (function (x) {
Sq + x*x;
});
Console.log (sq);//1+9=10

Five. Multidimensional arrays
JavaScript does not support a true multidimensional array, and can be approximated by an array of arrays;
Example: Analog 9*9 multiplication table
var table = new Array (10);
for (var i =0; i<table.length; i++) {
Table[i] = new ARRAY[10];
}
for (var row = 0; row < table.length; row++) {
for (Var col=0;col<table[row].length;col++) {
Table[row][col]=rol*col;
}
}
Console.log (Table[5][7]);//35

Six. Array methods
1.join (): concatenation of array elements into a string; Returns the resulting string, which is the inverse of string.split (); The array has no split () method.
2.reverse (): Returns the array in reverse order. Note that the original array is modified instead of generating the new number;
3.sort (): Returns the sorted array;
1. In alphabetical order when no parameters are available (temporarily converted to string comparisons if necessary);
2. If the undefined function is included, it will be queued to the tail of the array;
3. If you want to customize the rule, you need to pass in the function; If the first parameter is before, it should return a number less than 0;
Example 1:
var a = [33,4,1111,222];
A.sort ();
Console.log (a);//alphabetical order [1111, 222, 33, 4]
A.sort (function (x, y) {
return x-y;
// });
Console.log (a);//[4, 33, 222, 1111]
A.sort (function (x, y) {
return y-x;
})
Console.log (a);//[1111, 222, 33, 4]

Example 2:
var b =[' Ant ', ' Bug ', ' cat ', ' Dog '];
B.sort (function (x, y) {
var a = X.tolowercase ();
var B = y.tolowercase ();
if (a>b) {
return 1;
}else if (a<b) {
return-1;
}else{
return 0;
}
})
Console.log (b);//["Ant", "Bug", "Cat", "Dog"
4.concat (): Returns a new array consisting of the original array and the parameters of the method;
Note: 1. If the parameter is an array, concatenate the elements in the arrays, not the array itself;
2. Arrays that do not recursively flatten arrays;
3. The array of calls is not modified;
Example:
var a = [n/a];
Console.log (A.concat (4,5));//[1, 2, 3, 4, 5]
Console.log (A.concat ([4,5]));//[1, 2, 3, 4, 5]
Console.log (A.concat ([4,5],[6,7]));//[1, 2, 3, 4, 5, 6, 7]
Console.log (A.concat ([4,5],[6,[7,8]));//[1, 2, 3, 4, 5, 6, Array (2)]
5.splice (): A common method for inserting or deleting elements, depending on the parameters, there are several situations:
1. Only one parameter: Delete the element starting from this parameter (including) until the end, and return an array consisting of the deleted elements;
such as: var a = [1,2,3,4,5,6,7,8];
Console.log (A.splice (4));//[5,6,7,8]
2. Two parameters: The first parameter is the starting position, the second parameter is the number of deleted elements (length), returns an array consisting of the deleted elements;
such as: var a = [1,2,3,4,5,6,7,8];
Console.log (A.splice (2,2));//[3,4]
3. Pass in three and above parameters: Perform first delete, then insert function. The first parameter specifies where to start the deletion and insertion, the second parameter is the length of the deletion, and the third and subsequent parameters are inserted as an array element;
such as: var a = [1,2,3,4,5,6,7,8];
Console.log (A.splice (2,3,[2,3],4));//[3, 4, 5]
Console.log (a);//[1, 2, Array (2), 4, 6, 7, 8]
Console.log (a.length);//7

6.push (): Adds one or more elements at the end of the array (used as a stack) and returns the new array length;
Pop (): Deletes the last element of the array, reduces the length of the array (delete does not reduce the array length), and returns the deleted value; The method has no arguments, and the call deletes only one element at a time;
Note: All two methods Modify and replace the original array instead of generating a new one.
Example:
var a = [];
Console.log (A.push);//return array length 3
Console.log (A.pop ());//Returns the deleted value 3
Console.log (A.pop ());//2
Console.log (a.length);//1

7.unshift (): Adds an element from the head of the array, and the parameter is added as an array element;
Shifte (): Removes an element from the head of the array without parameters;
Example:
var a = [9];
Console.log (A.unshift);//Add elements to the head of the array and return the array length 4
Console.log (a);//[1, 2, 3, 9] prove to be added in the head
Console.log (A.shift ());//Returns the deleted element 1 (deleted from the head)

8.toString (): Converts each element of an array into a string and outputs a comma-delimited list of strings;
Tolocalstring (): Concatenate these strings into the final string using a localized (and custom implemented) delimiter;
Example:
Console.log ([1,2].tostring ());//1,2
Console.log ([1,2].tolocalestring ());//1,2

JavaScript Note 4-arrays

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.