Introduction to JavaScript Arrays
Arrays in JavaScript are different from those in other languages, mainly reflected in:
The items stored in the array can be of different types of data
The size of the array changes dynamically. When adding or removing items, the size can be dynamically changed to accommodate the current data item.
Create an array in JavaScript
There are two ways to create an array in JavaScript:
One: Call the constructor of the array
var a = new Array(3);//Create an array of length 3
var a = new Array(1,2,3)//Create an array with 1,2,3 content
In addition, when creating an array through a constructor, the new keyword can also be omitted, and the effect is the same as with new
Second: Create using literal method (creating an array using this method will not call the constructor of the array)
Var a = [1,2,3]
Determine whether an object is an array
There are two ways to determine whether an object is an array in JavaScript:
Properties and methods of JavaScript arrays
Length attributes
In JavaScript, the length property of an array returns the length of the array, which can be read or written
length-i: Delete i elements from behind to front
length+i: add i elements at the end of the array (all are underfine before initialization)
Stack method
Push method:
Pop method:
Array non-Pop method returns the last item in the array (note: it only returns the last item without affecting the original array)
Queue method
Shift method: Remove the first item of the array and return it, while decrementing the length of the array by 1.
Using push() and shift() methods in combination, you can operate arrays like queues.
Unshift method: add any item to the front end of the array and return the length of the array
Using unshift() and pop() methods can use the operation array like operating in reverse queues.
Array sorting method
Sort method sorts arrays (according to ASCII code table)
In addition, you can also specify a comparison function for sort()
Reverse():
Without calling reverse, manually implement reverse order:
How to operate
concat() method: if there is no parameter, return a copy of the current array, if the parameter is an array, add each item in the parameter array to the end of the current array copy, and then return that copy, if the parameter is not an array, add the parameter to the end of the current array copy and return that copy
slice() method: Return one or more items in the current array as a new array (does not affect the current original array)
splice() method: (Note: This method operates in the original array)
Position methods indexof() and lastindexof()
Indexof(startindex,item): In the current array, startindex (if there is no startindex, search from scratch) searches for item from front to back, and returns the index of the first item. If no item is found, return -1
Lastindexof (startindex, item): The usage is the same as indexof, but search from behind to front
Method of reducing
reduce and reduceright:
reduce(callback, reduce the initial value of the base (optional)). The callback function can accept four parameters (pre, cur, index, arr), representing the previous value, current value, item index, and array object. The return result of this function is automatically passed as a parameter to the next item.
Reduceright() is the same as reducee, but reduceright iterates from behind to front
Iteration method
The above article is a cliché about the usage of JavaScript arrays. I hope it can give you a reference and I hope you can support Wulin.com more.