Introduction to JavaScript arrays
Arrays in JavaScript are different from arrays in other languages, mainly in:
The items stored in an array can be of different types of data
The size of the array is dynamic and can be changed dynamically when the item is added or removed to accommodate the current data item
Create an array in JavaScript
There are two ways to create an array in javascript:
One: Calling the constructor of an array
var a = new Array (3);//Create an array of length 3
var a = The new Array (1,2,3)//Creates an array whose contents are 1,2,3
Also, you can omit the new keyword when you create an array from the constructor, with the same effect as with new
Second: Using literal values to create (using this method to create an array does not call the array's construction method)
Var a = [1,2,3]
Determines whether an object is an array
There are two ways to determine whether an object is an array in javascript:
The properties and methods of JavaScript arrays
Length Property
The length property of an array in JavaScript returns an array of lengths, which can be read or written
Length-i: Remove an array from the back-forward element
Length+i: Adds an I element to the tail of the array (underfine before initialization)
Stack method
Push method:
Pop method:
Array non-pop method returns the last item in the array (note: Just return the last item without affecting the original array)
Queue method
Shift method: Moves the first entry of the divisor group and returns, with the length of the array minus 1
Using the push () and Shift () methods, you can manipulate arrays like queues
Unshift method: Add any items to the front of the array and return the length of the array
Use the Unshift () and Pop () methods to manipulate arrays like a reverse queue
Sorting Methods for arrays
The sort method sorts the array (in the ASCII code table)
In addition, you can also specify a comparison function for sort ()
Reverse order function reverse ():
Manually implement reverse order without calling reverse:
Operation method
Concat () Method: Returns a copy of the current array if there are no arguments, and if the argument is an array, adds each item in the parameter array to the end of the current array copy, and then returns the copy, if the argument is not an array, adds the parameter to the end of the current array copy and returns the copy
Slice () method: Returns one or more items in the current array as a new array (without affecting the current original array)
Splice () Method: (Note: This method operates in the original array)
Location methods indexof () and LastIndexOf ()
Indexof (Startindex,item): startindex in the current array (search from scratch if no startindex) from the go after the item, and return the index of the first item, if no query to item then return-1
LastIndexOf (Startindex,item): Use with indexof, but search from the back forward
Reducing methods
Reduce and Reduceright:
Reduce (callback, reduce the base initial value (optional)), the callback function can accept four parameters (Pre,cur,index,arr), respectively, the previous value, current value, item index, array object, the return result of this function is automatically passed as an argument to the next
Reduceright () uses the same reduece, but the reduceright is iterative from the forward
Iterative methods
The above cliché of the use of JavaScript arrays is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.