13.1 The difference between C and JS arrays

C language array:

  • Elements have the same data type
  • Continuous memory storage
  • Access elements by numeric subscript
  • Memory:

Js arrays: Arrays that are completely simulated by objects

  • Element data types can be different
  • It doesn’t have to be contiguous, but if the value is an object, there’s an address in memory
  • You can’t subscript numbers, you can subscript strings
  • Memory:

13.2 Creating an Array

let arr = [1.2.3]
let arr = new Array(1.2.3) / / [1, 2, 3]
var arr = new Array(3) // [empty × 3
Copy the code

How do I split a string to generate an array?

var arr = '1, 2, 3'.split(', ') / / / "1", "2", "3"]

var arr = '123'.split(' ')    / / / "1", "2", "3"]


/ / 2. Use Array. The from
Array.from('123')  / / / "1", "2", "3"]

Array.from(123)   / / []

Array.from(true)  / / []

Array.from({name:'frame'}) / / []

Array.from({0:'a'.1:'b'}) / / []

Array.from({0:'a'.1:'b'.length:2}) //["a", "b"] the resulting array is a pseudo-array

Array.from({0:'a'.1:'b'.length:1}) //["a"]
Copy the code

What is a pseudo-array?

There is no prototype of an array in the pseudo-array, which is the POP () Push () API

Pseudo-array generation method:

  • Object add length, forcibly generated:Array. The from ({0: 'a', 1: 'b', length: 2}) / / / "a", "b" that generated arrays is false
  • var divList = document.querySelectorAll('div')You get a pseudo-array

How do I join two arrays

Arr. Concat (arr2) returns a new array, unchanged

The use of the slice

  • Section:

    var arr = [1.2.3.4.5.6.7.8.9]
    
    // Enter the starting coordinates
    arr.slice(2)  //[3, 4, 5, 6, 7, 8, 9]
    
    arr  //[1, 2, 3, 4, 5, 6, 7, 8, 9]
    
    // Enter 0 for shallow copy
    arr.slice(0)  //[1, 2, 3, 4, 5, 6, 7, 8, 9]
    Copy the code

13.3 Deleting an Element from an Array

Mentally retarded delete: Deletes elements but leaves them empty

var arr = ['a'.'b'.'c']
delete arr[0]  //true
arr   //[empty, "b", "c"] length does not change, delete is a mental retarded
Copy the code

Such arrays are called sparse arrays and are only bug-generating

Don’t change length arbitrarily


Correct deletion of posture:

  • Delete the first element:.shift()

  • Delete the last one: .pop( )

  • Replace the middle element:.splice(begin, length, item)

###13.4 Array traversal

  • Retarded in: prints elements other than length

  • It’s good to iterate with for

  • ForEach traverses the array

For is the same as forEach except for a break continue

For is the keyword, block-level scope, and forEach is the function scope


Iterate over the number of errors encountered

Cross-border undefined

###13.5 Find elements

  • indexOf(): Finds elements by index
  • .find((x)=> (x%5) === 0)) : Find the element according to the condition, : the element that x divisible 5,The element is returned
  • .findIndex((x)=> (x%5) === 0)) : Find the element according to the condition, : the element that x divisible 5,The index is returned

13.6 Other common apis

  • Direct ARr [100] = 100, equivalent to directly change length, become sparse array

  • Add elements: header adds:.unshift. tail push middle.splice

  • Reverse array: Changes the original array

Flipping strings through arrays:

  • Sort array :.sort

    • Specific writing:

    • Change the original array

    • Quick way to write:

###13.7 Array transforms

  • Reduce eats everything and digests it into a single thing, such as array summation :0 is the initial value.

  • Reduce can replace Map, filter:

  • Cats and dogs, children that turn into animals