Arrays are very important in any programming language, because functions represent the maximum functionality to be implemented, and arrays are part of the memory that these functions want to manipulate.
1 building an array
The flexibility of JS and other non-scripting languages is that to achieve a goal it may have a number of ways, such as the definition of an array of 2, the definition of the object is generally 2, and the class is a variety, a lot of tricks, the purpose is to save memory, suitable for different application scenarios
1) using constructors
var arr=new Array ();
2) Use literal
var arr=[];
2 detecting array types
In the front we have a non-reference typeof, which applies to all types, is a broad-spectrum type detection, and also is instanceof belongs to the object type detection, local type detection, there are special detection such as isNaN, for the array can also use instanceof,
But for multi-frame pages There may be problems (the reason I do not know, the book said, specifically not touched), so it is like isNaN with the Array.isarray () function for safe detection, and isNaN is different from the IsArray is the static function of the array
And isNaN is the function of window.
3 Conversion
The ToString () method of the array: its implementation procedure is to invoke the ToString () method of each element and then use ', ' to concatenate the resulting string, or to use the connector parameter in ToString () such as: ToString (' | | ')
The Tolocalstring () method of an array: the same as the ToString () method
The valueof () method of an array: the same as the ToString () method
4 using the array as a stack
The concept of the stack is advanced, LIFO, and array provides push,pop two ways to support the operation of this data structure
5 using the array as a queue
Shift function: Take the first element out of the array, and reduce the length by one, it is very simple to implement FIFO
Unshift function: Inserts one or more elements to the first position in reverse, so you can implement a reverse queue with pop mates
6 Array Sorting
Array sorting method sort (): Array default sorting principle, the array item is converted to a string, then the string is compared, so this default string comparison to sort is basically not get the correct result;
The array provides a sort and reverse method that literally understands the functionality of the 2 methods.
Sort supports custom comparisons where sort () can pass a function that receives 2 formal parameters, and the default comparison is in ascending order, that is, from small to large, then how do you compare it?
If the front is less than the back 1, if the equivalent returns 0, here accept return 1 o'clock front of the value1 put back, that is, ascending case who small who in front of the corresponding return-1
Special case: For the array items are all numeric type can be directly used subtraction operation;
7 Array Query
There is an entry in the query array, and the array provides indexof and lastindexof; the principle is that the object to be looked at and the array item to do the same judgment, here is strict, must be congruent, if found to return the array subscript, not found return 1, The corresponding lastindexof from the back to the front of the reverse lookup;
8 Array Iterations
Array iterations provide similar LAMDA expressions to the IList iteration method in C #, where the function expressions of these iterative methods all accept 3 parameters, item, item itself, index of the position where it is located, and array is the array
That is, function (Item,index,array) {};
Every (): that is, each function results in a return of true, the final is true,
Every (function (Item,index,array) {
Some operations
return true;
})
Filter: Returns true which items are newly formed into an array, and the purpose is to filter which items satisfy the condition;
foreach (): The function does not return a value, that is, each item simply executes a function to
Map (): That is, each item runs the function, and the result of the run consists of an array as the return value, which is equivalent to processing an array to get a new array;
Some (): that is, each item runs the function, and any function returns true, which means that the array satisfies the condition as long as any one of the array entries satisfies the condition
9 Array Management
Redo JS Virtuoso Third bullet: Array