Callable method in array
Stack method:
push()
Add the last element of the array
var colors = ["pink"]; add = colors.push("red", "blue"); alert(add); //3 alert(colors); //pink,red,blue
pop()
Delete last element of array
var colors = ["pink", "red", "blue"]; sub = colors.pop() alert(sub); //blue alert(colors); //pink,red
Queue method:
shift() vs. push()
push to add the last element of the array, shift to delete the first element of the array
var colors = ["pink", "red", "blue"]; sub = colors.shift(); alert(sub); //pink alert(colors); //red,blue
unshift() vs pop()
unshift adds the first element of the array, pop deletes the last element of the array
var colors = ["pink", "red", "blue"]; add = colors.unshift("perple"); alert(add); //4 alert(colors); //perple,pink,red,blue
Reorder method:
reverse()
Sort the array in descending order. (not flexible enough)
var values = [0, 5, 15, 10, 1]; a =values.reverse(); alert(a); //1,10,15,5,0
sort()
Convert the array to a string and sort the strings in ascending order.
PS: if you want sort to sort numbers in ascending order, you need to add a custom compare method to sort():
function compare(value1, value2) { return value1 - value2; }
Then call sort(compare) to sort the numbers in ascending order;
String ascending sort of sort
var values = [0, 5, 15, 10, 1]; a =values.sort(); alert(a); //0,1,10,15,5
sort in ascending order
function compare(value1, value2) { return value1 - value2; } var values = [0, 5, 15, 10, 1]; a =values.sort(compare); alert(a); //0,1,5,10,15
Operation method:
concat()
Creates an array element and merges it to the last position of the current array
var values = [0, 5, 15, 10, 1]; a =values.concat(16, 18); alert(a); //0,5,15,10,1,16,18
slice()
Create an array and copy it to the newly created array:
slice(1,4): copy the array element of the first subscript to the element of the third (does not contain the fourth array subscript) slice(1): copy elements except for subscript 0
var values = [0, 5, 15, 10, 1]; a =values.slice(1, 4); alert(a); //5,15,10
var values = [0, 5, 15, 10, 1]; a =values.slice(1); alert(a); //5,15,10,1
splice()
Delete, insert and replace the array:
Splice (starting from the ith position subscript, processing several elements, inserted and replaced elements) splice(2, 0, "red", "blue"); / / in the second subscript Start inserting red and blue elements splice(0,2); / / delete two elements at the beginning of subscript 0
delete
var values = [0, 5, 15, 10, 1]; a =values.splice(1,2); alert(a); //5,15 alert(values); //0,10,1
insert
var values = [0, 5, 15, 10, 1]; a =values.splice(1,0,6,8); alert(a); //empty alert(values); //0,6,8,5,15,10,1
replace
var values = [0, 5, 15, 10, 1]; a =values.splice(1,1,6,8); alert(a); //5 alert(values); //0,6,8,15,10,1
Location method
indexOf()
Find backward from the beginning of the array (position 0)
lastIndexOf()
Find from the end of the array forward
var numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1]; alert(numbers.indexOf(4)); //3 alert(numbers.lastIndexOf(4));//5
Iterative method
every()
If every item passed in satisfies the expression, it returns true, otherwise it returns false
var numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1]; var everyResult = numbers.every(function (item, index, array) { return item > 2; }); alert(everyResult); //false
some()
If there is a satisfied expression passed in, true will be returned, otherwise false will be returned
var numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1]; var everyResult = numbers.some(function (item, index, array) { return item > 2; }); alert(everyResult); //true
filter()
Filter the satisfied expression and return a new array
var numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1]; var everyResult = numbers.filter(function (item, index, array) { return item > 2; }); alert(everyResult); //3,4,5,4,3
map()
Perform the same algorithm operation on all items, and then return a new array
var numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1]; var everyResult = numbers.map(function (item, index, array) { return item * 2; }); alert(everyResult); //2,4,6,8,10,8,6,4,2
forEach()
Perform the same operation for each item of the array, such as traversing each item, that is, traversing the array:
var numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1]; var everyResult = numbers.forEach(function (item, index, array) { console.log(item); });//The background output is as follows /* 1 2 3 4 5 4 3 2 1 */
Merging method
Both of them are summation, but the direction of entry from the array is the opposite during the summation operation.
reduce()
Prev is the previous value, the default is 0, and cur is the value of the current item in the current array. prev,cur,index,array
var numbers = [1, 2, 3, 4, 5]; var sum = numbers.reduce(function (prev, cur, index, array) { return prev + cur; }); alert(sum); //15
reduceRight()
It is the same as reduce, but in summary, the traversal array starts from the end 5 and goes forward, which is the reverse of the traversal array of reduce
var numbers = [1, 2, 3, 4, 5]; var sum = numbers.reduceRight(function (prev, cur, index, array) { return prev + cur; }); alert(sum); //15
Values that can be returned by each method after a method call:
values = push("566");
push and unshift return the number of arrays after the operation;
pop and shift return the deleted elements after the operation;
reverse, sort and concat return the array after the operation;
slice returns the array copied after the operation;
splice returns the elements deleted after the operation;
The boolean value is returned after every and some operations;
The new array is returned after the filter, map and forEach operations;
forEach returns the output of the method after using the array items;
reduce and reducereight return the summarized value;