Basic Introduction to Javascript Array, javascript array
Javascript, a magic language, has a unique array. We need to go to its dregs, extract its essence, and summarize common best practices. In case of any errors, please note.
Javascript array is an array of objects with the characteristics of objects. If the attribute name is a small continuous integer, an array is used. Otherwise, an object is used.
Array source
All arrays are constructed by arrays. Let's test the constructor attribute.
var arr = [];arr.constructor === Array; // truearr.constructor === Array.prototype.constructor; // true
Create an array
// Array literal method var arr1 = [1, 2, 3]; // [1, 2, 3] // constructor method var arr2 = new Array (); // [] empty Array var arr3 = new Array ('9'); // ["9"] A string element var arr4 = new Array (9 ); // [] the Array length is 9var arr5 = new Array ([9]); // [[9] is equivalent to two-dimensional Array var arr6 = new Array (1, 2, 3); // [1, 2, 3] var arr7 = new Array (1, function f () {}, {o: 6}, null, undefined, true ); // array can store any mixed data type
Because of the arr4 method, when only one numeric parameter is passed to the Array constructor, the constructor returns an empty Array with the length attribute set. Therefore, we recommend that you use the array literal type, which is short and concise.
Check whether the object is an array
var arr1 = [1, 2, 3];typeof(arr1); // object
As we all know, typeof cannot correctly detect the type.
arr1 instanceof Array; //true
The instanceof method is no problem in one web page. Once other web pages are nested, there are two global scopes, and the detection during mutual calls will lead to problems.
Array.isArray(arr1); // true
Array. isArray () is a new method of ECMAScript5, with no defects. The only problem is that ie8 is not supported, and ie9 is not supported in strict mode.
Object.prototype.toString.apply(arr1).slice(8, -1); // Array
The last method is the best method of the detection type.
Array Length
The length of the array is also its attribute. Increasing the length will not cause an out-of-bounds error.
The length value is equal to the integer attribute name with the largest array plus 1.
Var arr1 = []; arr1 [9] = 1; // an array containing only one element
Setting a small value will delete the attribute whose name is greater than or equal to length.
If the length value is set to 0, it is equivalent to clearing the array.
var arr2 = [1, 2, 3, 4, 5];arr2.length = 3; // [1, 2, 3]arr2.length = 0; // []
Array Traversal
Do not use for in to traverse arrays in a loop, because for in will traverse all attributes of the prototype chain, but we don't need that much. We recommend that you use the for loop method.
Var arr1 = [1, 2, 3]; arr1.test = 9; // for in mode for (var prop in arr1) {cosole. log (prop, arr1 [prop]);} // The output is as follows // 0 1 // 1 2 // 2 3 // test 9 // for loop mode for (var I = 0, len = arr1.length; I <len; I ++) {console. log (arr1 [I]);} // The output is as follows // 1 // 2 // 3
We can see that the for in method has an additional test value, which can be ruled out using the hasOwnProperty function, but it will be much slower than the for loop method.
It is necessary to cache the length of the array. Each access has performance overhead (the latest browser has been optimized in this regard ).
Summary
This section briefly introduces the basic knowledge of Array, so that you can have a more comprehensive understanding of Array. Next, we will introduce the Array method.
Although Javascript has a lot of things that are not easy to understand, as I have been learning for a long time, I have gradually fallen in love with it (because there is no sister to let me love it ).
Articles you may be interested in:
- JS array details
- Javascript Array. remove () Array Deletion
- ArrayToJson converts an array into json format js Code
- Function for js to determine whether an array is used: isArray ()
- How to transmit Array objects in JS to the background in JSON format
- Notes for sorting arrays (sort) in js
- Example of Array usage in JS
- The example explains how to operate the Array in JS.
- Sort JS Array processing functions
- Summary of adding and deleting elements to and from Array objects in JavaScript Array
- Js uses Array. prototype. sort () to sort Array objects