Array [array]

A. [Value type and reference type of data]

Value type: Number string Boolean NULL undefined

Reference types (complex data structures) : function [] {}

Value types are stored on the stack

Reference data types are stored in heap space

Value type assignment is passing the corresponding value directly

The reference data type is passed the corresponding address

        var a = 1;
        var b = a; //b=1
        console.log(a, b);
        a=2;
        console.log(a,b);
        var obj1 = {
            name:"Zhang".age:18
        }

        var obj2 = obj1;// The reference data type assignment is passed as an address belonging to a shallow copy
        console.log(obj1.age,obj2.age);

        obj1.age=28;
        console.log(obj1.age,obj2.age);
        // Reference data types directly using == to compare addresses
        console.log(obj1==obj2)
Copy the code

B. [Array definition]

An array stores a group of data of the same data type

1 Grammar sugar by way of literals

    var arr1 = [1.2.3.4.6.4];  
Copy the code

2. Through new

    var  arr2 = new  Array(1.2.3.4); 
Copy the code

3 constructors

    var  arr3 = Array("1"."2");
Copy the code

When defining an array, you can pass an array in literal format

    var arr4 = new  Array([1.2.3.4.6.4]);
Copy the code

5 When defining an array, you can specify the array length

    var arr5  = new  Array(5); // 5 is the length of the array
Copy the code

Other array knowledge:

The [typeof] operator can determine the data type

            console.log(typeof(arr1),typeof(arr2),typeof(arr3));

            // length Specifies the length of the array
            console.log(arr1.length);

            // The element in the array can be accessed by the index number

            console.log(arr1[0]);

            // Reassign the elements in the array
            arr1[0] = 23;
            console.log(arr1[0]);

            // Add a new element to the array
            arr1[6] = Awesome!; 
            console.log(arr1);
            // Delete elements by modifying the length of the array
            arr1.length = 3;
            console.log(arr1);

            // Use the array for loop to traverse

           for(var i =0; i<arr1.length; i++){console.log(arr1[i]);
           }
Copy the code

C. [Common apis for arrays (emphasis)]

Array traversal API

Define an array:

           var arr = [1.2.3];
           var arr = new Array(a);Copy the code

Array length:

    arr.length
Copy the code

ForEach, every, some, map, filter, for of

     foreach(function(item,index,arr){})
     every(function(item,index,arr){ return item>1}) 
Copy the code

Return Boolean true only if all conditions are met

     some(function(item,index,arr){ return item>1}) 
Copy the code

Boolean returns true as long as there is one that meets the criteria

    map(function(item,index,arr){ return item*10})  
Copy the code

Returns the processed array

    filter(function(item,index,arr){ return item>10}) 
Copy the code

Returns a new array of elements that match the criteria

For of iterates through a number array

    for(item of  arr.entries()){console.log(item)} ; 
Copy the code

Add elements: [posh, unshift, splice]

Push (arg) Add element to end of array returns length of new array unshift(arg) Add element to beginning of array returns length of new array splice(arg1,arg2,arg3...)// Arg3 follows the element to be inserted
     //arg1 is the position where insertion or deletion begins
     //arg2 indicates the number of deletions
Copy the code

Delete elements: [pop, Shift, splice]

    pop()
Copy the code

Removing an element from the end of an array returns the deleted element

    shift() 
Copy the code

Deleting an element from the beginning of the array returns the deleted element. Note that the index of the element that follows immediately changes

    splice(arg1,arg2)  
    Arg2 indicates the number of deletions
Copy the code

Modify elements:

  arr[i] = 23;
Copy the code

Array reverse: [reverse]

    reverse();
Copy the code

Array sort: [sort]

    sort(function(a,b){returnA-b}) in ascending orderCopy the code

Whether the array contains an element:

    includes(item);  / / return true/false
    indexOf(arg1,arg2) 
    The index arg2 starts with returns that the element's index has no -1
Copy the code

Array splicing:

    contact(arr); 
Copy the code

Array copy:

    copyWithin()
Copy the code

Array to string:

   join("*") // Delimiters can be specified
   
   toString() 
Copy the code

Check whether the object is an array:

    Array.isArray();
Copy the code

Pseudo-array to array:

    Array.from(arr);
Copy the code

Array retrieval:

    find(function(item,index){return item>10}) 
Copy the code

Find the first element that matches the criteria

    findIndex(function(item,index){return item>10}) 
Copy the code

Finds the index of the first element that matches the criteria

Array interception:

   slice(starNum,endNum);
Copy the code

Note that does not includeendNumlocation

D.【 Classical algorithm: Bubble sort 】

[2,3,1,6,5] –> [1,2,3,5,6]

How bubble sort works: Compare in pairs

function bubble(arr) {
            var temp;
        // The first loop of the outer loop finds the maximum value in the array and puts it at the end of the array
        // The end of the second loop of the outer loop finds the next-to-last value and places it in the next-to-last array

            for (var i = 0; i < arr.length - 1; i++) {
                for (var j = 0; j < arr.length - i - 1; j++) {
           // Compare two adjacent values. If the next value is less than the previous value, the data is exchanged
                    if (arr[j + 1] < arr[j]) {
                        temp = arr[j];
                        arr[j] = arr[j + 1];
                        arr[j + 1] = temp; }}}return arr;
        }
        Var arr = [2,3,1,6,5];
        // console.log(bubble(arr)) ;
Copy the code

【 extension: Selection sort 】

function fastSort(arr) {
            var minNum ;// Define a minimum value
            var index = 0;// Define a variable to record the minimum index
            var temp = 0;
            for (var j = 0; j < arr.length - 1; j++) {
                minNum = arr[j];  
                // The inner loop is used to find the minimum between j and arr. Length
                for (var i = j; i < arr.length; i++) {

                    if(minNum > arr[i]) { minNum = arr[i]; index = i; }}// Swap the minimum value into arr[j]
                    temp = arr[j];
                    arr[j] = arr[index];
                    arr[index] = temp;
            }
            return arr;
        }
        var arr = [2.3.1.6.5];
        console.log(fastSort(arr));
Copy the code

(Arrays can be one of the most important knowledge of JavaScript to master!!)