Debut - "Data Structure and Algorithm Analysis - Arrays"


I was originally a sports student at the Guangzhou Institute of Physical Education, and then I came into contact with the front-end through some coincidences, which was very interesting. Lines of code can write a cool web page and complete awesome user interactions. So I decided to step into the field of it and conduct cruel hair loss practice. I have been learning about it for half a year. Today is the first blog I have written, and I intend to record some of my experiences in the journey of hair loss, and also make a summary for myself.

Arrays are a data type often encountered in JS. Arrays can store the same series of data, or they can store values ​​of different data types in the array.

First summarize some core methods of arrays: {

            1. concat concatenates two or more arrays and returns the result              

            2. join joins all elements of an array into a string

            3. indexOf returns the index of the first and the given parameter, otherwise it returns -1

            4. lastIndexOf returns the last index with the given parameter, otherwise -1

            5. sort sorts the elements of an array 

            6. toString returns the array as a string

            7. valueOf returns an array as a string

            etc

                }


Create an array var arr=[ ];
var arr1=new Array( );
var arr2=new Array(5);//Create an array of specified length

You can also use elements to initialize the array: var arr3=[1,2,3,4,5,6,7];//The following uses this array as an example

If you want to know how many elements there are in an array, you can use the length property of the array. For example arr3.length above // ​​7

If you want to access the element at a specific position in the array, you can use the array brackets to add the subscript of the element, such as arr3[0]//The result is 1. Array subscripts are counted from 0

To output all elements of an array, we can iterate over the array

                    var num=' '//declare an empty variable   

                    for(var i=0;i<arr3.length;i++){

                      num+=arr3[i]

                     }

                    console.log(num)//output all elements of the array

Add and delete elements of an array:

1. Add elements: add element push() at the end, add unshift() at the head, and add splice at a specified position (index, removelength, item1..., itemN),

Adding elements can also be directly assigned to the array, such as arr3[8]=2, which is equivalent to adding the number 2 to the end of the array

2. Delete elements: delete the tail and return the element pop(), delete the element shift() at the head, delete the splice at the specified position (index, removelength)

The parameters index and removeindex in the splice() method are required. index is the specified element position, removelength is the number of deleted elements (if 0 is filled in, it will not be deleted), item1--itemN is the added element, which can have N values.

3. Array merge: concat( ) method: arr3=[1,2,3,4,5,6,7];

             newarr=[8,9];

             arr3.concat(newarr)//Merge two arrays, output arr3=[1, 2, 3, 4, 5, 6, 7, 8, 9]

4. Join method: concatenate the array into a string and return it. arr3.join('—')//will return "1-2-3-4-5-6-7"

5. Sorting of arrays: Given an unordered array narr=[1,3,5,7,3,2,6,2]; we can directly use narr.sort() to sort without problem. The resulting output is //[1, 2, 2, 3, 3, 5, 6, 7]

But if it is narr1=[1,3,21,33,52,6] using narr1.sort() to sort the output result is //[1, 21, 3, 33, 52, 6]

Why is this happening, because the sort method defaults the elements to strings for comparison when comparing

So since all the numbers are passed in, we can give him a method, so write narr1.sort(function(a,b){

                            return a-b 

                          })//In this way, the a and b passed in during sorting will be compared. If a>b, then a positive number will be returned, otherwise, a negative number will be returned, and 0 will be returned if they are equal. The sort method will sort according to the returned value.

If it is not clear then we can write function arrSort(a,b){

                if(a>b){

                  return 1}else if(a<b){

                       return -1}else{

                           return 0}

              }

              narr1.sort(arrSort)//The sort method calls the previously defined arrSort() function and then uses it to sort the array.

6. Search: There are two methods, namely indexOf and lastIndexOf. Here we redefine the array var arr=[1,2,3,4,5,7,1]; then we use two methods to search for the number 1 in the array and see the output

arr.indexOf(1)//The return index is 0, this method returns the first index that matches the given element

arr.lastIndexOf(1) returns the index is 6, this method returns the last index that matches the given element

7. There are also some methods such as forEach, slice, and every that have not been introduced, and will be written later.

 

Finally, write a small example: Knowing that the first number of the Fibonacci sequence is 1, the second number is 2, starting from the third item, each item is equal to the sum of the previous two items, and find the first 20 of the sequence number.

var num=[];

num[1]=1;

num[2]=2;

for(var i=3;i<20;i++){

num[i]=num[i-2]+num[i-1]

}

for(var i=0;i<num.length;i++){

console.log(num[i])

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325258710&siteId=291194637