This article describes the characteristics and practical applications of JavaScript arrays. Share it for your reference, as follows:
JavaScript provides an object similar to array characteristics, which turns the subscript of the array into a string as an object's attribute. Although it comes slower than a real array, it is very convenient to use.
1 array literal
An array literal is an expression that surrounds zero or more comma-separated values in a pair of square brackets:
var empty = []; var numbers = [ 'zero', 'one', 'two', 'three' ]; (empty[1]);//undefined (numbers[1]);//one ();//0 ();//4
Array objects inherit from, so numbers inherit a lot of useful methods.
JavaScript allows arrays to contain values of any mixed type:
var misc = [ 'string', 11.3, false, true, null, undefined, ['nested', 'array'], {object: true}, NaN, Infinity ]; ();//10
2 length
The array has a length attribute, but it has no upper bound. If an element is stored using a number equal to the current length as a subscript, the length attribute value will be increased to accommodate new elements without array crossing.
The value of the length attribute is very strange. It is the maximum integer attribute name plus 1 in the array, that is, it is not necessarily equal to the number of elements in the array:
var myArray = []; ();//0 //myArray only contains one attribute, but the value of length is equal to the attribute name of the largest integer of this array plus 1myArray[100000] = true; ();//100001
[] The post-operator converts the expression it contains into a string, and if the expression has a toString() method, it will be called. This string is treated as the attribute name. If the string is a positive integer that is larger than the current length value and is less than 4294967295, then the length of the array will be reset to the new subscript plus 1.
You can directly set the value of length. Setting a larger length value will not allocate more space to the array; and setting length smaller will cause all attributes with a subscript that are equal to the new length to be deleted:
//Delete the element = 3; (numbers);//[ "zero", "one", "two" ]
By specifying the subscript as the current length of the array, you can append a new element to the end of the array:
//Add new elementsnumbers[] = 'four'; (numbers);//[ "zero", "one", "two", "four" ]
usepush()
The same function can be achieved more easily:
//Add new element (push)('good'); (numbers);//[ "zero", "one", "two", "four", "good" ]
3 Delete
JavaScript arrays are objects, so they can be useddelete
Remove elements:
delete numbers[2]; (numbers);//[ "zero", "one", <1 empty storage location>, "four", "good" ]
Unfortunately, this will leave a hole in the array! What we hope is that after deletion, the subsequent elements of the deleted element will automatically move forward.
JavaScript arrays providesplice()
Method, it accepts the following parameters:
①. Index of the array.
②. The number of elements to be deleted.
③. Other parameters (multiple) - Insert these parameters into the index position in turn.
//The first parameter is the index number, and the second parameter is the number of elements to be deleted(2, 1); (numbers);// [ "zero", "one", "four", "good" ]
4 enumerations
JavaScript arrays are objects, so you can use the for in statement to iterate through all properties of the array. However, the for in statement cannot guarantee the order of attributes, and unexpected attributes may be obtained from the prototype chain.
You can use for to avoid the above problems:
var i, myArray = numbers; for (i = 0; i < ; i += 1) { (myArray[i]); }
5 Arrays and Objects
How to choose between these two? When the property name is a small and continuous integer, use an array; otherwise use an object.
JavaScript's typeof will think that the array is an object, which makes no sense!
We can define a function to detect arrays:
var is_array = function (value) { return value && typeof value === 'object' && === Array; };
This method fails in an array constructed in a different window (window) or frame. So please use the following better method:
var is_array = function (value) { return (value) === '[object Array]'; }; (is_array(myArray));//true
6 Methods
JavaScript provides some methods available for arrays, which are stored inmiddle. We can add a method to the array to calculate the array:
//Add a new method to calculate elements in an array('reduce', function (f, value) { var i; for (i = 0; i < ; i += 1) { value = f(this[i], value); } return value; });
In this way, all arrays inherit this method. It accepts a function with the initial value as a parameter. Then iterate through the array, calculate the new value through the function, and finally return this value.
//Create a number arrayvar data = [9, 16, 32, 192, 8]; //Define addition and multiplication functionsvar add = function (a, b) { return a + b; }; var mult = function (a, b) { return a * b; }; ((add, 0));//257 ((mult, 1));//7077888
Because arrays are objects, you can also add methods to the array directly:
= function () { return (add, 0); }; (());//257
Because the string "total" is not an integer, the length value of the array will not be changed. When the property name is a string, it becomes the property of the array.
7 Specify the initial value
If you use the new array obtained with [], it will be empty. This is if you access it, you will get undefined. We can implement a method that can initialize array element values:
//Specify the initial value for each element = function (dimension, initial) { var a = [], i; for (i = 0; i < dimension; i += 1) { a[i] = initial; } return a; }; //Create an array containing 10 0svar myArray = (10, 0); (myArray);//[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
The elements of JavaScript arrays can be arrays, and the multidimensional array function is implemented in this way:
//Array of arrayvar matrix = [ [0, 1, 2], [3, 4, 5] ]; (matrix[1][0]);//3
The Array method can be extended so that it can initialize the matrix:
/** * * @param m Number of first dimension * @param n Number of second dimensions * @param initial initial value */ = function (m, n, initial) { var a, i, j, mat = []; for (i = 0; i < m; i += 1) { a = []; for (j = 0; j < n; j += 1) { a[j] = initial; } mat[i] = a; } return mat; }; //Construct 0 filled 4*4 matrix((4, 4, 0)); //Construct the unit matrix = function (n) { var i, mat = (n, n, 0); for (i = 0; i < n; i += 1) { mat[i][i] = 1; } return mat; }; var myMatrix = (4); (myMatrix); (myMatrix[3][3]);//1
Interested friends can also use this siteOnline HTML/CSS/JavaScript code running tool:http://tools./code/HtmlJsRunTest the above code running results.
For more information about JavaScript, you can also view the special topic of this site: "Summary of JavaScript array operation skills》、《Summary of JavaScript characters and string operation techniques》、《Summary of JavaScript traversal algorithm and skills》、《Summary of JavaScript search algorithm skills》、《Summary of JavaScript mathematical operations usage》、《Summary of JavaScript data structure and algorithm techniques"and"Summary of JavaScript Errors and Debugging Skills》
I hope this article will be helpful to everyone's JavaScript programming.