The stack and queue of a JavaScript array series

Source: Internet
Author: User

The so-called arrays (English: Array), is an ordered sequence of elements. If a collection of variables of the same type is named, the name is an array name. Each variable that makes up an array is called the component of an array, also known as an element of an array, sometimes called a subscript variable. ---Baidu Encyclopedia

Simply understood, an array is an ordered list of data.

Array is one of the most commonly used data types in JavaScript, and arrays in JavaScript, like other languages, are an ordered list of data, but unlike other languages, it can be used to hold any data type, and the length of the array can be dynamically adjusted.

var elements = ['first', 2, {age: 14}];
Create an array

Now that you know the array, let's look at how to create an array! Javascript provides two ways to create arrays.

1. Create from a constructor function

var arr1 =NewArray ();//[]
var arr2 =new array (3);  //[,,]  create an array with three items
var arr3 = new array (3, 4)  //[3, 4]
VAR ARR4  = new array ( " 3 ');  //[' 3 ']
var arr5 = new array ( ' 2 ', //[' 2 ', ' 3 ']

Seeing the results above is simply compelling, so it can be seen that the difference in parameter passing produces a different result, and the result is surprising, but we can conclude that if you pass in a parameter of a numeric type, you create an array of the fixed items (for example, ARR2), and if you pass in other types of arguments, An array containing the current parameters (for example, ARR4, ARR5) is created,

This method is not used to create an array, so we will continue to see how the next method creates the array.

2. Create by literal amount

var arr1 = [];
var arr2 = [1, 2];
var arr3 = ['1', '3'];

It is so simple, rough, and clear; it is the most common method of creating arrays, and this method is very efficient to create.

How to access and set up an array

It says how we create an array, and we need to access or modify the elements in the array after it's created. Let's take a look at it.

    • Accessing an array, accessing the elements in an array through an index, starting at 0 , what if the index of the access is greater than the number of elements in the array? The result is definitely not found, so it will return to undefined.

When it comes to the number of elements of an array we cannot fail to mention a property of the array, which is the length property, which returns the number of elements in the current array.
And the length property is not a read-only property, it can be modified, so we can add elements of the array by using the Length property, and we can also delete the elements of the array.

var arr = [' First ',' Second ',' Three '];
Console.log (arr[0]); //first
Console.log (arr[1]); //second
Console.log (arr[3]); //undefined

Delete an element with length
Arr.length = 2;
Console.log (arr); //["First", "second"]

Add elements with length (this method can be used to add elements at the end of the array)
Arr[arr.length] = ' five ';
Console.log (arr); //["First", "second", "five"]
How to behave like a "stack"

To know how an array behaves like a stack, let's first look at what a stack is.

Here we do not delve into the "stack", if we go down to the 1:30 we also say not clear, nor is the focus of our article.

In simple terms, "stack" is a data structure, a LIFO (last-in-first-out) of the database, that is, LIFO, the most recently added elements were first moved out.
Adding data and deleting data to the stack is also known as push-in and pop-up, and push-in and eject only occur at the top of the stack.

Stack pictures

Javascript provides an array of two methods to allow us to implement the "stack" behavior, let's take a look at it.

    • Push: Adds an element to the end of the array and can pass in any number of arguments
    • Pop: Removes the last item from the end of the array, returning the deleted element so that the length-1 of the array
var arr = ['A', 'B', 'C'];
arr.push('D');
console.log(arr); //['A', 'B', 'C', 'D']
var rem = arr.pop();
console.log(arr);//['A', 'B', 'C']
console.log(rem); // 'D'
How to behave like a "queue"

Last we said that the array in Javascript can have the same behavior as "stack", then it can also show the same behavior as "queue".

A "stack" is a LIFO data structure, and a "queue" is a FIFO (first-in-a-out), which is a pre-in-and-out.

The data adds elements at the end of the queue, moves out of the element at the front, says that we can use the push method to add elements at the end of the array, so what is the way to move the elements out of the front of the array? The answer is the shift () method.

Queue

    • Shift: Delete the first element of the array, return the deleted element, and make the array length-1

You can see that it's just the opposite of the pop () method

 var arr = [ ' A ',   ' C ']; 
Arr.push ( console.log (arr);  //[ ' A ',   ' B ',   ' C ',   ' D ']
Var rem = arr.shift ();
console.log (arr); ' B ',   ' C ',   ' D ']
Span class= "hljs-built_in" >console.log (REM);  //  A '

We can see above we are adding elements at the end, front-end delete elements, but we can not add elements at the front end, delete elements at the end? In other words, we can simulate the "queue" in reverse.

Javascript provides us with a method and then we can implement the reverse simulation with the Pop () method. That is Unshift ().

Queue 2

    • Unshift (): Add any number of elements at the beginning of the array
var arr = ['A', 'B', 'C'];
arr.unshift('D');
console.log(arr); //[ 'D', 'A', 'B', 'C']
var rem = arr.pop();
console.log(arr);//['D', 'A', 'B']
console.log(rem); // 'C'
Summarize

The importance of the

array, which is the most common reference type data structure in Javascript, is self-evident, so " Array "also has a lot of methods, if a space written down will be very very long, so not only not conducive to reading and also very test the reader on patience, long, so we have two articles to introduce, if you are interested in the article, you can continue to pay attention, if you like it, you can also forward, praise, the author is also a support !

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: