Arrays and objects (object) should be the two most frequently used types in JavaScript, and array provides a number of commonly used methods: stack method, queue method, reordering method, operation method, location method, iterative method and so on.
1, the Stack method of array
The stack is a LIFO (Last-in-first-out, LIFO) data structure, which means that the most recently added items were first removed. The insertion (push) and removal of items in the stack occurs only in one position-the top of the stack. The ECMAScript provides a push () and Pop () method that enables you to implement a stack-like behavior. The following two diagrams illustrate the stack and stack operations respectively.
The push () method can receive parameters for arbitrary data, add them to the end of the array one by one, and return the modified array length. The Pop () method removes the last item from the end of the array, reducing the length value of the array
var students = [];
Students.push ("Bluce", "Jordan", "Marlon", "Kobe");//into Stack 4
alert (students.length); 4
alert (students[0]); "Bluce", the first item at the bottom of the stack
alert (students[1]); "Jordan"
Students.push ("Paul");
alert (students.length); 5
var item = Students.pop ();//"Paul"
Alert (students.length); 4
2, array of the queue method
Stack data structure access rules are LIFO (LIFO), and the queue data structure access rules are FIFO (first-in-first-out, first out). The queue adds an item at the end of the list and removes the item from the front of the list. The push () method is a way to add an item to the end of the array, so simulating the queue requires only one method--shift () that takes the item from the front end of the array, and it can move the first item in the divisor group and return the item, along with the length-1 of the array. Using the shift () and push () methods, you can use arrays just as you would with queues.
var students = [];
Students.push ("Bluce", "Jordan", "Marlon", "Kobe");/Team 4//students=["Bluce",
"Jordan", "Marlon", "Kobe"];
alert (students.length); 4
alert (students[0]); "Bluce", the first item at the bottom of the stack
alert (students[1]); "Jordan"
Students.push ("Paul");
alert (students.length); 5
//students=["Bluce", "Jordan", "Marlon", "Kobe", "Paul"];
var item = Students.shift (); "Bluce"
alert (students.length); 4
//students=["Jordan", "Marlon", "Kobe", "Paul"];
In addition, ECMAScript provides a unshift () method that can add any item to the front of the array and return the length of the new array. Therefore, combining the unshift () and Pop () methods, you can simulate queues in the opposite direction, adding items to the front of the array, removing items from the end of the array