Arrays should be data types that are used extremely frequently in every language, and JavaScript is no exception.
Personally, the array type in JS is very powerful.
There is no need to specify the length of the array (not mutable) when the array is initialized without a language such as C + +.
There is also no need to specify a specific base data type (Number,string,boolean,null,undefined,object), which means that a number of data types can be stored in an array.
I would like to summarize the common methods of array, before this, first introduce the basic usage of array:
Basic usage:
1. Create an array:
// create an empty array var arr = []; var New Array (); // creates an array of a specified length (not much meaning, because the length is variable) var New Array (ten); // create an array with an initial value var arr = ["Ling", "Jia", "Wen", +]; var New Array ("Ling", "Jia", "Wen", 18);
2, the basic operation of the array:
//array values are based on the 0 start numeric indexConsole.log (arr[2]);//> "Wen"Console.log (Arr[4]);//>undefined//Get Array lengthConsole.log (arr.length);//>4//Change Array lengthArr.length = 3; Console.log (arr[3]);//undefined (should have been output)//clever use of array lengths to add elementsArr[arr.length] = "Man"; Console.log (arr[4])//>man//adding elements using the Push methodArr.push ("arr") Console.log (arr)//> Ling,jia,wen,18,man,arr
3. Array and String conversions:
1) The array is converted to a string type:
arr1 = Arr.tostring (); // "ling,jia,wen,18"arr2 = Arr.join ("-"); // "Ling-jia-wen-18"
2) string to array:
var str = "ling,jia,wen,2017"; var arr = Str.split (","); // ["Ling", "Jia", "Wen", "_"]arr[3] = Arr[3].parseint (); // ["Ling", "Jia", "Wen")
push, pop, shift, Unshift methods
1. Basic usage:
Push () End insert
Pop () End Delete
Shift () Delete first item
Unshift () First insert
Arr.push ("man") // ["Ling", "Jia", "Wen", +, "man"]arr.pop () // ["Ling", "Jia "," Wen ",arr.shift () // [" Jia "," Wen ",]arr.unshift (" Hello ") // ["Hello", "Jia", "Wen",)
2. Simulation Data structure:
Analog Stacks (FIFO):
Push (): Into the stack
Pop (): Out of stack
Analog Queue (advanced back-out):
Forward queue:
Push (): Incoming queue
Shift (): Out of queue
Reverse queue:
Unshift (): In queue
Pop (): Out of queue
Splice method
This method is particularly powerful and has to be taken out individually, and it can be cleverly deleted, inserted, and replaced in the array.
How do we do that?
This method can be understood to receive three parameters: start position, delete item number, insert item content
(Here I take all the insert items as a parameter, which may actually be multiple parameters)
Understanding these three parameters is critical. Understanding, you can implement delete, insert, and replace operations.
Take a look at the following example:
vararr = ["Ling", "Jia", "Wen", 18]//Implementing a Delete operation//Delete 2 items arr-> ["Wen", starting from item 1th)//remove:["Ling", "Jia"]//That is , it will return the item you deleted, which is almost not seen in someone else's blog, do not understand why?? varremove = Arr.splice (0,2)//implementing an Insert Operation//starting with item 1th, delete 0 items, insert items as "Hello" and "World"//arr->["Hello", "World", "Wen",Arr.splice (0,0, "Hello", "World")//Implementing the Replace Operation//starting with item 2nd, delete 1 items, insert 1 items//arr->["Hello", "Jia", "Wen",Arr.splice (1, 1, "Jia")
The code is clearly written.
When deleting , do not insert anything, that is, omit the third parameter;
Set the delete item to 0 when inserting ;
When replaced , the substitution is implemented as long as the deleted item and the number of inserts are the same;
Iterative methods
Array arrays have 5 iterations of the method. They accept two parameters:
1. A function that runs every iteration (the function accepts three parameters: the value of the current item of the array, the index of the current item of the array, the array itself )
2. Function Scope object (can be omitted)
every ()
For each item of the array, execute the given function, and if the function returns true for each item, the method returns True
Chestnuts:
var numbers = [1,1,1,2,1,1,1]; var everynum = Numbers.every (function(item, index, array) { return (item = = = 1 //falsevar numbers1 = [1,1,1,1,1,1,1]; var everynum = Numbers1.every (function(item, index, array) { return (item = == 1//true
Some ()
The some method corresponds to the Every method, and the relationship between the two is like or and and
For each item of the array, execute the given function, and if the function returns true for any one of the items, the method returns True
Chestnuts:
var numbers = [1,1,1,2,1,1,1]; var everynum = Numbers.some (function(item, index, array) { return (item = = = 2 //true
Filter ()
For each item of the array, execute the given function if some item of the function returns True, then the method returns True if the item is a re-formed array (a bit around)
Direct Chestnut better understand:
var numbers = [1,2,3,4,5,6,7,8,0]; var filternum = Numbers.filter (function(item, index, array) { return (item >= 4//[4,5,6,7,8]
This method is appropriate for the condition query of the array.
ForEach ()
This iterator believes that everyone is not unfamiliar, is simply enumerated, and then dosomething ();
// 2 4 6 8var numbers = [1,2,3,4];numbers.foreach (function(item, Index, array) { Console.log (item*);});
Map ()
For each item of the array, execute the given function, returning the array of function call result re-composition
Chestnuts:
// [2, 4, 6, 8] var numbers = [1,2,3,4]; var mapnum = Numbers.map (function(item, index, array) { return item* 2;}); Console.log (mapnum);
OK, I know and can think of the array inside is almost so much, there is nothing missing welcome to add!
An array of getting started with javascript: array type detailed