Use JS to implement those data structures (array 01)

Source: Internet
Author: User

Before we start the formal content, I have to talk about the data types and structure in JS, and some confusing concepts. So why do we start with an array? Array in JS is the most common memory data structure, the array of data structures in JS has a lot of methods, many beginners do not remember most of the use of arrays, only know push,pop,shift and so on the most basic several. Therefore, this series (array) will be as far as possible to let you have a thorough understanding of the group. It also facilitates the learning and use of other data structures behind it.

It's possible that many web front-end developers have a question about whether arrays and objects are data types. Or a data structure? Then we take this question, start the following study, hope after reading this article, your vague concept will become clearer.

First, in JS, there are two types of data, basic type (primitive type) and complex type, where the base type is:string(String),number,Boolean (Boolean), plus undefined and null. A complex type is a objecct(object).

There may be some questions about this, and there are only six types of them. The array , the regular (REGEXP), what is the date ? They are all a branch of object, in other words they belong to the object type, which is exactly the place where JS is different-everything objects. And the following to talk about including queues, stacks, lists, sets, trees, graphs and other data structures in JS display, also through the object and prototype to achieve. This article has no intention to describe in detail the types of data and the type of structure and the form of representation in JS. So donuts.

The story has begun, please fasten your seat belts and follow me on this vast expanse of land-- arrays .

Let's first explain what an array is, the so-called array, which is an ordered sequence of elements . If you name a collection of variables of the same type, the name is called the 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. The numeric number of each element used for the area fraction group is called the subscript. An array is a form of organizing a number of elements of the same type in an unordered form in the program design for ease of handling. these unordered collections of homogeneous data elements are called arrays. Simply put, An array is a collection of data that is used to store multiple identical types. (Of course, the array in JS can also store different types of data, but!) Do not recommend this! )

First, the creation and initialization of arrays

  I believe a lot of small partners know it's easy to create an array:

var arr = [];

So we create an array, and we can create and initialize an array with the New keyword:

// create an empty array var New Array (); // creates an array of a specified length var New Array (4); // creates an array with the specified arguments var New Array (1,2,3,4);

Of course, the way you create and initialize arrays with the New keyword is not recommended, just to introduce you. In fact, the essence of the first way we create an array is to instantiate an array object with new. OK, here is not much to say its implementation principle, or return to the array itself.

So how do we read the data in the array? Very simple, I have a sentence, that is, through the brackets ([]) arr[2], to pass the position of the value, get to the corresponding position of the value, you can also be re-assigned in this way.

Second, the array of additions and deletions

Next talk about how to use the JS array to implement the method to achieve the end of the array toadd and remove: push (insert element at the end of the array), Unshift (array header insert Element), Pop (delete element at the end of the array) and shift (array header delete element)

1. Push method

If I don't want to use the push method, is there any way to insert an element at the end of the array? It's really simple, we just have to assign the value to the element on the last empty space in the array.

var nums = [0,1,2,3,4= 5;

We get the length of the array by the Long property is 5, but our array corresponding subscript is starting from 0, in this way, also to the end of the array to insert a new element. Of course, we can make it easier to use push to insert an element at the end of an array:

var nums = [0,1,2,3,4];nums.push (5);

You can also get the same results. Of course, push can also pass in multiple parameters, sequentially inserting an array from the tail:

var nums = [0,1,2,3,4];nums.push (5,6,6); // [0,1,2,3,4,5,6,6]

2.Unshift Method

So again, how do you add an element to the head of an array without using the native method?

var nums = [0,1,2,3,4,5,6];  for (var i = nums.length;i >= 0;i--) {  = nums[i-1];      } // [Undefined, 0, 1, 2, 3, 4, 5, 6]nums[0] = 1; // [-1, 0, 1, 2, 3, 4, 5, 6]

In fact, by looping through loops, we add one for each bit in the nums array, that is, to move one bit backwards, which causes the position of the head to be vacated (its position is present), but at this point we do not assign a value to the subscript of the vacated position, So it increases the length of the value really undefined, after the assignment, will get the result we want.

Here we still use the Unshift method to insert a new value to the head of the array:

var nums = [0,1,2,3,4,5];nums.unshift (-1); // [-1, 0, 1, 2, 3, 4, 5]nums.unshift ( -2,-3); // [-2, -3,-1, 0, 1, 2, 3, 4, 5]

Note, then, that when using unshift to pass in multiple parameters, he will put the first parameter in the head of the array (and so on), that is, the Unshift method will insert all the parameters in the order of the array, not as we take it for granted, from the first parameter added to the array in sequence.

3.Pop method

If I want to delete the elements at the end of the array, we can use the Pop method, in fact, we can still use JS to simulate a pop:

var nums = [0,1,2,3,4,5= nums.length-1; // [0, 1, 2, 3, 4]

By manually reducing the length of the array by one bit, we can delete the elements at the end of the array, and, of course, reduce the two bits to three bits.

In fact, the pop method is often used in daily development to delete elements at the end of an array (thepop () method has no parameters, just deletes the elements at the tail of the array. ):

var nums = [0,1,2,3,4,5];nums.pop ()//  [0, 1, 2, 3, 4]

  4. Shift method

Then let's look at how to delete an element from the first array:

var nums = [0,1,2,3,4,5];  for (var i = 0; i < nums.length; i++) {    = nums[i + 1]}//  [1, 2, 3, 4, 5 , undefined]

As you can see, our last is undefined, that is, in the last loop, I + 1 refers to a position in an array that has not been initialized (it opens up space but is not assigned), so that way it simply overwrites the previous bit's value, and does not actually delete the element. If you want to delete the first element, you need to use the shift method.

var nums = [0,1,2,3,4,5];nums.shift (); // [1, 2, 3, 4, 5]

  5. Splice method

Finally, let's look at how to use the splice () method to add and remove elements anywhere in the array:

var nums = [0,1,2,3,4,5,6,7];nums.splice (2); // [0, 1] // If you add only one parameter, the description removes all subsequent elements starting with subscript 2
var nums = [0,1,2,3,4,5,6,7];nums.splice (2,1); // [0, 1, 3, 4, 5, 6, 7] // If you add two parameters, you delete several elements that begin with subscript 2. 
var nums = [0,1,2,3,4,5,6,7];nums.splice (2,1, "a", "B", "C"); // [0, 1, "A", "B", "C", 3, 4, 5, 6, 7] // three or more parameters, which means removing the 1 (second parameter) element from the beginning of subscript 2 (the first parameter) and adding all parameters after Subscript 2, starting with the third argument, setting the second argument to 0 so that you can add elements from anywhere without deleting the element 

To say a little more, we can also use the delete operator to delete the elements in the array, but in fact, delete simply deletes the value stored on the corresponding subscript, and does not simultaneously delete the storage worth of space, causing the value on the corresponding location to be undefined:

var nums = [0,1,2,3,4,5,6,7]; Delete nums[2]; // [0, 1,  undefined, 3, 4, 5, 6, 7]

Then some basic usage of the array is introduced here, there should be about two or so length to introduce the array. It is really very important to spend so much space, and please don't worry. When the real learning of the array, and then to see the stack, the queue of this data structure, in fact, it is very simple.

Finally, because my level is limited, the ability and the great God is still very far apart, if there are errors or unclear, but also hope that everyone is not hesitate to correct. Thank you so much!

Use JS to implement those data structures (array 01)

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.