A method for declaring arrays

1. Array literals

var arr1 = [];
Copy the code

2. Use the built-in Array constructor to declare arrays

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

3.

Remove the new:

var arr3 = Array(a);Copy the code

This is not often used

conclusion

Arr1. __ prototype__ –> all three refer to Array –> all arrays inherit from Array. Prototype –> All Array methods and attributes inherit from Array. Objects, like arrays, have all three methods, and all three point to Object –> All objects inherit from Object.prototype

Index value index

  1. In fact, the underlying mechanism of the array in JS is to inherit the object, the key name of the array is ignored, but the element can still be accessed by index, so in JS array is another form of object, their underlying mechanism is the same
  2. Index: Marks the inner element of an array, the index of the array elements

2.1 Sparse array

I put a comma after the array, just like I didn’t

If you put a lot of commas in the middle, you need to pay attention to the length


var a = [,,];
console.log(a.length); / output2I put a comma after the array, just like I didn'tvar b = [,1.2,]  
console.log(b.length); / output3, which has no value but still occupies a positionCopy the code

Such arrays are called sparse arrays that have many commas but no value in front of them

2.2 Constructors to declare arrays

1. Use the built-in Array constructor to declare arrays that cannot use sparse arrays

var arr = new Array(,2.2.2)/errorCopy the code

2. Fill the constructor parentheses with only the length of the number you want to create the array

var arr = new Array(5);
  console.log(arr.length); / output5
  console.log(arr); / output [,,,,,], that is5When there are two or more digits, it is normalvar arr = new Array(1.5);
  console.log(arr.length); /2
  console.log(arr); / [1.5]
Copy the code

3. The string must be enclosed in quotation marks

var arr = new Array(a); / No quotation marks, error; After declaring a, print1And [undefined]
  console.log(arr.length);
  console.log(arr);
Copy the code

3. Array methods

Array methods include: push, unshift, etc. Like objects, Array methods inherit the prototype property of the Array constructor.

3.1 Adding arrays

Push (a): Adds an element to the last bit of the array. Unshift (a): Adds an element to the first bit of the array

On the console: The return value of push, unshift, and so on is the length of the array after the method is executed. (Interview highlights)

var arr = [2.3.4]
Copy the code

On the console: arr.push[5] –> print out 4

3.2 Array deletion

Pop: cut off the last bit of the array, and return shift: Cut off the first bit of the array, and return pop(), shift ()

3.3 Array flashback

The reverse () parentheses also don’t need arguments

3.4 Clipping an array

Format: arr.splice(index of the start item, length of the cut, the last data added after the cut)

Example 1

var arr  var arr = ['a'.'b'.'c'];
    arr.splice(1.1.1.2.3.4.5);
    console.log(arr); / [output"a".1.2.3.4.5."c"]
Copy the code

If index is 1, cut 1 bit and add 1, 2, 3, 4, 5

Example 2

 var arr = ['a'.'b'.'c'.'e'];
    arr.splice(3.0.'d');
    console.log(arr); / [output"a"."b"."c"."d"."e"] 
Copy the code

Example 3

    var arr = ['a'.'b'.'c'.'e'];
    arr.splice(-1.0.'d');
    console.log(arr); / [output"a"."b"."c"."d"."e"]
Copy the code

Index -1: indicates the last digit. I’m going to start at minus 1, and I’m going to start at 0

Thinking summary

Splice (index position of newly added element, length to be deleted, data to be added) For example in example 2: arr.splice(3, 0, ‘d’) : Add ‘d’ to index position of newly added element, and the length to be deleted is 0

Note: It needs to be based on the original array, as in example 3: arr.splice(-1, 0, ‘d’) : The original position in the array was -1, plus ‘d’

Create a splice function:

  function splice(arr,index){
       if(index >= 0 ){
         index += 0;
       }else{
         index +=arr.length;
       }
       return index;
  }
Copy the code

That’s how splice () is created inside the system

3.5 Sorting arrays

If there are no arguments, sort(): sort in ascending order; [27,49,5,7] sort()

If there are arguments, sort(a, b): determines the order of the return values from a to b.

If the return value is positive, put a first,

If the return value is negative, b comes first.

If the return value is 0, hold

Ascending and descending

Since sort() by ASICII code does not satisfy the normal sort requirements we want, we create a function that combines the properties of sort() with bubble sort to solve the normal sort problem:

   var arr = [27.49.5.7];
    arr.sort(function (a, b) {/27with49In front of the row;27with5than5Front row;27with7than7Front row...if (a > b) {
        return 1; / return a positive value, before row B}else {
        return -1; / return negative value, front of row A}})console.log(arr);
Copy the code

If (a < b) = if (a < b)

Random sequence

Math.random(): Generates a decimal at random: 0< math.random () <1

  var arr = [27.49.5.7];
      arr.sort(function (a, b) {
        if (Math.random() - 0.5 > 0) {
          return 1; / return a positive value, before row B}else {
          return -1; / return negative value, front of row A}})console.log(arr);
Copy the code

conclusion

All of these methods modify the original array.

2. Small knowledge

var a = [1.2.3]
 console.log(a[3]);
Copy the code

Items that are not in the array are printed as undefined

The empty item also returns undefined, since it has no value but takes up memory