The concept of an array: the referenced object type.
- Essence: a plurality of data stored in memory space, and then a name.
- Data structures: different data structures, different good operation.
- An array of features: easy to find and maintain data.
Creating an array:
- Method 1: var array element name = [1, element 2, element 3 ...... n] element;
- Note: You can create an empty array, it is recommended to create an assignment at the same time!
- Mode 2: var array name = new Array ();
An array of Use:
- Index: Each element has a room number for the default starting from 0, incremented by one.
- Element: in fact, saved in a data array, can be a variety of data types.
- Have access to an array of elements: the use of elements in the array and ordinary variables exactly the same.
- Use array name [subscript]: Gets the underlying data in the correspondence.
- Using the array name [index] = new value: the new value replaces the original value.
An array of attributes:
- .length attribute: record the number of array elements theoretically. The actual saving is the last element at +1 standard array. The number and the actual length of the element is not necessarily the same.
- Note: length from the array can not be used alone, do not need to manually modify automatically maintained.
Common operations:
- Get the last element in the array: arr [arr.length-1];
- Get the penultimate element of the n-th elements: arr [arr.length-n];
- At the end of the array pursue a new element:. Arr [] = arr.length new element;
- Modify the array's length property, you can achieve the purpose of deleting elements!
- Removes the last element in the array: arr.length--;
- Delete the last n elements of the array: arr.length- = n;
- About the nature of the array: type of reference object
- Output: dir: structure and contents of the output data, rather than immediately output, click on the small triangle after the output of the right content in memory!
- Traversal: each element of the array in order to find, and do the same!
for (var i=0;i<=arr.length;i++) {
arr[i];
}
Array Category:
- Indexed array: array subscript numbers
- Associative array: Custom index value
- Create an array: var arr = []; // create an empty array; arr [ "subscript name"]; = value; add a new element to an empty array, and a custom index names.
- Using arrays: general arrays as elements, use the same general array. Value: arr [index name] assignment: arr [index] = Name the new value;
- Note: length attribute associative array failure, always 0;
- Traversal: for in looping through an associative array!
for(var key in arr) {
arr[key];
}
Notes: key: for each cycle to save the underlying variables under temporary variables; in: Keywords; arr: array name needs to traverse an array
7. Summary: an array of features: easy to maintain and search data!
8. associative array vs indexed array: associative array: Find a faster and more efficient. The reason: direct positioning element, the search speed is not the number of elements influence; indexed array: relatively slow! The reason: Only traversal search, search efficiency by the number of elements, the position of influence!
API array
- API: others have written, the program we used directly.
- Array: storing a plurality of data, and the operation of these API objects to provide data.
- String into an array
3.1 string(arr); arr.toString();
3.2 API can be more than two indexed array to a string. ------ each element in the array and then converted to string between "," stitching.
3.3 arr.join ( "splice symbol"); each element in the array of the specified character spliced splicing. Note: If the stitching is omitted, default use "," mosaic.
3.4 fixed routine:
var str="<ANY>"+arr.join("</ANY><ANY>")+"</ANY>"
elem.innerHTML=str;
4. Select the array splice
4.1 splicing: splice multiple elements or array to a new array;
var new_arr=old_arr.concat(值,数组);
Note: concat (); return new array, without modifying the original array; if required stitching is an array, the array elements to break one element in the mosaic!
4.2 Select: selection element between the positions specified in the array, a new array returned composition.
var new_arr=old_arr.slice(starti,endi);
Note: Do not modify the original array
If an API parameters are two subscripts, typically containing free end of the head!
slice(starti,starti+n);//n表示选取元素的个数
Support negative argument: the principle of using the length property.
arr.slice(starti,-n)<=>arr.slice(starti,arr.length-n);
Not all API supports negative parameters, use arr.length-n.
The second parameter is omitted: Select from the starting position up to the end.
var new_arr=arr.slice();
While omitting two parameters: copy the array;
var new_arr=arr.slice();
CRUD ---- splice 5. array
Increased 5.1: arr.splice (starti, 0, the value 1, value 2, value ...... value n-3);
Note: The new value is inserted after the specified position, all subsequent shift positions that order; if the value of the array is inserted, not broken, the specified position is inserted between the array.
5.2 Delete:
arr.splice(starti,n); //从starti位置开始删除n个元素
Note: directly modify the original array; support a negative argument; you can omit the second argument ---- has been deleted to the end; there is new data ---- return the deleted elements of value.
5.3 Review:
arr.splice(starti,n,值1,值2.....); 其实就是将原值删除,加入新值! //从starti位置开始,删除n个元素,在加入新值
Note: Deleting elements need not be consistent with the preservation number of elements inserted!
6. Sort:
6.1 Flip ---- Reverse:
arr.reverse(); //将元素组中的元素按原顺序颠倒!
Note: directly modify the original array, not return the new value; can not modify the content of the order, but the head and tail upside down!
Sequential ordering 6.2: arr.sort (comparator function); // according to the contents of the array to a small large / small to large by sequential ordering!
Note: The default parameter is not passed, the data in the array in ascending order; directly modifying the original array, not return the new value!
Principle: sort by default elements in the array into a string, and then follow the character encoding compare the size!
6.3 comparator functions: one by one comparison between the size of two elements.
format:
function comp(a,b){
return a-b;
}
If a> b, need to return a number greater than 0!
If a = b, the need to return a number equal to 0!
If a <b, a need to return the number of less than 0!
7. Stack: open end, a closed end array structure!
7.1 stack, the stack:
Stack: remove an element from the array.
Stack: add an element to the array.
Data structure with both ends open: 7.2 queue
8. The two-digit groups:
8.1
eg:var arr=[[1,2,3,4],[3,4,5,6],[6,7,8,9]];
8.2 traversing two-dimensional array: two steps: 1, to obtain a two-dimensional array of each sub-array; 2, traversing each subarray!
JavaScript some of the information
http://www.makeru.com.cn/live/1396_592.html?s=45051