1. Array object properties

The constructor property returns a reference to the array function that created this object. Object. constructor can be used to determine data types

    let da=new Array(a)if(da.constructor==Array) {console.log('This is an array.')// This is an array
    }
    if (test.constructor==Array)
      {
      document.write("This is an Array");
      }
      if (test.constructor==Boolean)
      {
      document.write("This is a Boolean");
      }
      if (test.constructor==Date)
      {
      document.write("This is a Date");
      }
      if (test.constructor==String)
      {
      document.write("This is a String");
      }
      //This is an Array
Copy the code

2. Length Indicates the length of the array. Object

3. The Prototype property gives you the ability to add properties and methods to objects. object.prototype.name=value

       function employee(name,job,born)
        {
        this.name=name;
        this.job=job;
        this.born=born;
        }

        var bill=new employee("Bill Gates"."Engineer".1985);

        employee.prototype.salary=null;
        bill.salary=20000;

        document.write(bill.salary);
        / / 20000
Copy the code

2. Array object methods

1. Concat () joins two data to form a new array arrayObject.concat(arrayX,arrayX,…… ,arrayX)

  // The first type can be multiple
   [1.2].concat([3.4])
   / / [1, 2, 3, 4]
  / / the second
  let a=[1.2]
  let b=[3.4]
  let c=[...a,...b]
  console.log(c)
  / / [1, 2, 3, 4]
Copy the code

2. Join () converts an array to a string in a certain format — comma by default

    [1.2.3].join()
    / / "1, 2, 3 '
    [1.2.3].join("|")
    / / "1 | 2 | 3"
Copy the code

The pop() method is used to delete and return the last element of the array.

   [1.2.3].pop()
   / / 3
Copy the code

4. Push () adds data to the array

let da=[]
let len =da.push(1.2.3)
console.log(da,len)
3 / / [1, 2, 3]
Copy the code

5. The reverse() method is used to reverse the order of elements in an array.

    [1.2.3].reverse()
    / / [3, 2, 1]
Copy the code

Shift () removes the first element in the array and returns the value of the first element

   let da=[1.2.3]
   let str=da.shift()
   console.log(da,str)
   1 / / [2, 3]
Copy the code

7. The slice(start,end) method returns selected elements from an existing array.

    [1.2.3.4].slice(2.3) left closed right open data [2.3)
    / / [3]
Copy the code

The 8.sort() method is used to sort the elements of an array. The default is to sort by character encoding order

    function sortNumber(a,b)
    {
    return a - b
    }

    var arr = new Array(6)
    arr[0] = "10"
    arr[1] = "5"
    arr[2] = "40"
    arr[3] = "25"
    arr[4] = "1000"
    arr[5] = "1"
    console.log(arr.sort(sortNumber))
    // ["1", "5", "10", "25", "40", "1000"]
Copy the code

The splice() method adds/removes items from/to the array and returns the deleted array. arrayObject.splice(index,howmany,item1,….. ,itemX)

  • Index: required. Integer to specify where items are added/removed, and negative numbers to specify positions from the end of the array.
  • Howmany required. Number of items to delete. If set to 0, the project will not be deleted.
  • item1, … ItemX is optional. The new item added to the array.
   let a=[1.2.3.4]
   let b=a.splice(1.2.5.6)
   console.log(a,b)
   / /,5,6,4 [1] [2, 3]
Copy the code

10. ToLocaleString () converts an array to a local string.

    [1.2].toLocaleString()
    / / 1, 2, ""
    [1.2].toString()
    / / 1, 2, ""
Copy the code

11. Unshift () adds one or more elements to the header of the array

   let a = [1.2]
   let b = a.unshift(3.4.5)
   console.log(a,b)
   / /,4,5,1,2 [3] 5
Copy the code

Return true if every() is true otherwise false

    [1.2.3].every(x= >x>0)
    //true
    [1.2.3].every(x= >x>1)
    //false
Copy the code

13. Return true if some() and every() are the opposite; otherwise return false

    [1.2.3].some(x= >x>2)
    //true
    [1.2.3].some(x= >x>3)
    //false
Copy the code

14. Filter () returns the filtered value

    [1.2.3].filter(x= >x>1)
    / / [2, 3]
Copy the code

15. ForEach () and map() are traversal groups

    [1.2.3].forEach(x= >console.log(x))
    / / 1
    / / 2
    / / 3
     [1.2.3].map(x= >console.log(x))
    / / 1
    / / 2
    / / 3
Copy the code

3. The ES6 new features

The array.from () method is used to convert two classes of objects into true arrays: array-like objects and iterable objects (including the new ES6 data structures Set and Map).

    //1 to convert a string to an array
    Array.from('123')
    / / [' 1 ', '2', '3']
    
    //2, object to array
    let arrayLike = {
    '0': 'a'.'1': 'b'.'2': 'c'.length: 3
    };
    let arr2 = Array.from(arrayLike);
    // ['a', 'b', 'c']
    let namesSet = new Set(['a'.'b'])
    Array.from(namesSet) 
   // ['a', 'b']
   
   //3* array. from can also take a second argument, similar to the map method of arrays, that is used to process each element and place the processed value into the returned Array.
    Array.from(arrayLike, x= > x * x);
    / / is equivalent to
    Array.from(arrayLike).map(x= > x * x);

    Array.from([1.2.3].(x) = > x * x)
    / / [1, 4, 9]
Copy the code

2.Array.of converts a set of values to an Array.

   Array.of(1.2.3)
   / / [1, 2, 3]
   Array.of(3)
   / / [3]
   Array.of(3).length
   / / 1
Copy the code

3. CopyWithin () Within the current array, copies the member in the specified location to another location (overwriting the original member), and returns the current array. That is, using this method, you modify the current array.

It takes three arguments.

  • Target (required) : Replace data from this location.
  • Start (Optional) : reads data from this position. The default value is 0. If it is negative, it is the reciprocal.
  • End (Optional) : Stops reading data until this position, which is equal to the array length by default. If it is negative, it is the reciprocal.
    [1.2.3.4.5].copyWithin(1.3)
    / /,4,5,4,5 [1]
    
    // copy bit 3 to bit 0
    [1.2.3.4.5].copyWithin(0.3.4)
    // [4, 2, 3, 4, 5]
Copy the code

4. The find () and findIndex ()

  • The find method of an array instance, used to find the first array member that matches the criteria. Its argument is a callback function that is executed by all array members until the first member that returns true is found, and then returned. If there is no qualified member, undefined is returned.
   [1.2.3, -2.3].find(n= >n<0)
   / / - 2
   [1.2.3.3].find(n= >n<0)
   //undefined
   [1.5.10.15].find(function(value, index, arr) {
      return value > 9;
    }) / / 10
    In the above code, the find callback can take three arguments: the current value, the current position, and the original array.
Copy the code
  • The findIndex method on an array instance is used much like the find method, returning the location of the first qualified array member, or -1 if all members fail.
    [1.5.10.15].findIndex(function(value, index, arr) {
      return value > 9;
    }) / / 2
Copy the code

5. The fill() method fills an array with the given value.

    [1.2.3].fill(0)
    / / [0, 0]
    new Array(3).fill(0)
    / / [0, 0]
    
    // The fill method can also accept the second and third parameters that specify where the fill starts and ends.

    ['a'.'b'.'c'].fill(7.1.2)// Open and close
    // ['a', 7, 'c']
Copy the code

5. Entries (), keys() and values()

  • ES6 provides three new methods — entries(), keys() and values() — for traversing groups of numbers. They both return a traverser object, which can be used for… The of loop is traversed, the only differences being that keys() is traversal of key names, values() is traversal of key values, and entries() is traversal of key value pairs.
    for (let index of ['a'.'b'].keys()) {
      console.log(index);
    }
    / / 0
    / / 1

    for (let elem of ['a'.'b'].values()) {
      console.log(elem);
    }
    // 'a'
    // 'b'

    for (let [index, elem] of ['a'.'b'].entries()) {
      console.log(index, elem);
    }
    // 0 "a"
    // 1 "b"
Copy the code

The 6.includes() method returns a Boolean value indicating whether an array contains a given value, similar to the includes method of strings. This method is in ES7, but the Babel transcoder already supports it.

    [1.2.3].includes(2);     // true
    [1.2.3].includes(4);     // false
    [1.2.NaN].includes(NaN); // true
Copy the code