This time I will bring youArrary method of JS, useArrary method of JSAttention What are the matters? Below are practical cases. Let’s take a look.
Stack method:
js provides two methods to implement stack-like operations: push(), pop()
The stack is A LIFO (last in first out) data structure,
var arr = [1,2,3,4]; arr.push(5); // result: [1,2,3,4,5]arr.pop(); // result: [1,2,3,4]
queuemethod:
The queue data structure access rule is FIFO (first in first out). The queue adds items at the end of the list and removes items at the front of the list. The method to achieve this operation is: shift()
var arr = [1,2,3,4]; arr.push(5); // result: [1,2,3,4,5]arr.shift(); // result: [2,3,4]
Reordering:
reverse() method implements reordering
var arr = [1,2,3,4,5]; arr.reverse(); // [5, 4, 3, 2, 1] // 另外一种重排序的方法Array.sort()
Splicing:
var arr = [1,2,3];var arr2 = [4,5,6]; arr.concat(arr2); // result: [1, 2, 3, 4, 5, 6]
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to the php Chinese websiteOtherrelated articles!
Recommended reading:
Detailed explanation of JavaScript timer
Events and callback functions of JavaScript running mechanism
Detailed explanation of the browser’s multi-threading mechanism
The above is the detailed content of Arrary method of JS. For more information, please follow other related articles on the PHP Chinese website!