In addition to the object type, array is the most commonly used type in ECMAScript.
Unlike other languages, each item of the ECMAScript array can hold any type of data.
Moreover, the size of the ECMAScript array can be dynamically adjusted, and the length of the array can grow automatically as the data is added.
How arrays are created
1. Using the array constructor
var colors1 = new Array();var colors2 = new Array(20);var colors3 = new Array("ggr",11,"grr");
2. Using array literals notation
var colors = ["red","gren"];var values = [1,2,];//强烈建议不要这样使用,因为在不同浏览器下,实现方式不同//在IE中,第三个值为undefined,在其他浏览器中,只会创建包含1和2的数组。
The length property of the array
This property is not read-only, you can set this property to remove an item from the end of the array or to add a new item to the array.
var colors = ["res","rer","gege"];colors.length = 2;alert(colors[2]);//undefined
detecting arrays
1. In a Web page or global scope, use instanceof to get it done.
value instanceof Array
2. If multiple frames appear in a Web page, there are actually more than two different global execution environments. To solve this problem, a method of Array.isarray (value) is provided. The method browser that supports Array.isarray () has ie9+,firefox4+.
The following summarizes these methods of the array
According to my own situation, the more familiar method, on the list, it is not explained in detail.
1. Conversion method
All objects have tolocalestring (), ToString (), and ValueOf () methods, where the ToString () method returns a comma-delimited string of strings that are stitched together as a string of each value in the array.
Instead, the call to the ValueOf method returns an array. If you do not see the call to the ToString () method, the ToString () method is called in the background. These three methods return array items by default in the form of a comma-delimited string, and if you use the Join () method, you can use a different delimiter. The join method of an array accepts only one parameter, which is a string used as a delimiter.
2. Stack-like methods: Pop and push, stack additions and deletions are done at the top of the stack, so it is a last-in-first-out data structure.
Push (push): You can accept any number of arguments, and then return the length of the modified array.
Pop (popup): You can remove the last item from the end of the array, reduce the length of the array, and return the deleted item.
3. In addition to the stack-like approach, of course, there is a queue-like approach: SHIFT and push. Queue is FIFO, add is at the end of the team, remove is at the head of the team.
Shift: Moves the first item in the array and returns the item.
Push: Is added individually at the end of the array.
Using shift and push together, you can use arrays like queues.
Unshift: Adds any element to the front of the array and returns the length of the array.
Pop: Deletes an item at the end of the array, returning the deleted item.
Combined with unshift and pop, you can reverse-emulate a queue.
4. Reorder methods: Reverse and sort, both of which return the sorted array.
Reverse: As the name implies, the order of the array is reversed.
Sort method: By default, the sort method sorts the array items in ascending order, calls the ToString () method of each array item, and then compares the resulting string to determine how to sort. The sort method compares strings, even if each item in the array is a numeric value.
So there are some problems when you sort the numbers, and you can accept a function.
5. How to Operate
Concat (): Creates a new array based on all the items in the current array, first creating a copy of the array, then adding the accepted parameters to the copy, and finally returning a newly constructed array.
var colors = ["ff","ffff"];var colo = colors.concat();console.log(colors);console.log(colo);console.log(colors === colo);
Slice (): Slice, accepts one or two parameters.
If there is only one argument, return this parameter to the specified position to all items at the end of the array.
If there are two parameters, which is the start and end positions, all items from the starting position to the end position are returned, but the end position is not included.
Slice also does not change the original array.
var colors = ["ff","ffff"];var colo = colors.slice(0,1);console.log(colors);console.log(colo);
If the parameter is a negative number, then the array length is added to each parameter and then returned.
If the first argument is greater than the second argument, an empty array is returned.
Splice () Method: This method is the most powerful method. The main purpose is to insert items into the middle of the array.
(1) Delete: Two parameters: Delete the starting position, delete the number of items.
(2) Insert: You can insert any number of items to the specified location. Three parameters, starting position, 0 (number of items to delete), the item to insert. If you insert more than one item, you can pass in the IV, fifth song parameter.
colors.splice(2,0,"geet","grrg")
(3) Replace: Any number of items can be inserted into the specified location, and any number of items will be deleted. Three parameters: The starting position, the number of items to be deleted, and any number of items to insert.
The items to be inserted and the number of items deleted are not necessarily equal.
In general, the parameters of these three methods are probably the same meaning, you can find a better way to remember. The first is the starting position (deleted or added), the number of items deleted, and the number of items inserted.
The change of this method will directly affect the original array. Splice returns an empty array if none of the deleted items are returned.
6. Location method: IndexOf (), LastIndexOf ().
Since it is a positional method, of course it returns the position of the element in the array. Both methods accept two parameters, the index of the entry to find, and, optionally, the starting position to start the lookup.
IndexOf is a lookup from the beginning of an array, and LastIndexOf is looking forward from the end of the array. Finally returns the index of the item in the array. If it is not found, return-1.
The common browsers that support both methods are: Ie9+,firefox2+,chrome.
7. Iterative Methods
A total of 5 iterative methods.
Every (),
Filter (),
ForEach (),
Map (),
Some (),
All accept such a function parameter:
function(item,index,array){ ...//}
Popular browsers that support these iterative methods have ie9+,firefox2+,chrome.
8. Merge method
Reduce (), reduceright (). The difference between these two methods is that reduce is iterated from the front of the array until the end. Reduceright () is iterated from the back of the array to the beginning of the array.
The following example illustrates
var values = [1,5,4,7];var sum = values.reduce(function(prev,cur,index,array){ return prev+cur;});console.log(sum);//17
The common browsers that support these two merge methods are: Ie9+,firefox3+,chrome.
Reference type of JavaScript (array type)