1.JS has no real array

\quad in JS, just use objects to simulate arrays, JS arrays are not typical arrays.

Typical arrays:

  • Elements have the same data type
  • Use continuous memory storage
  • Gets the element by numeric subscript

Array in JS:

  • The array type of the element can be different
  • Memory is not necessarily contiguous
  • You can’t subscript numbers, you can subscript strings. Although it looks like you can get an array object with a numeric subscript, it’s actually converted to a string.
  • So, an array in JS can have any key. Such as:arr["xxx"] = 1

2. Create an array

New array:

  • Let arr = [1, 2, 3]
  • Let arr = new Array(1,2,3)// 1,2, and 3 declare the contents of the string
  • let arr = new Array(3)// 3 declares the length of the string

From another type:

  • Let arr = '1, 2, 3. The split (',')
  • Array.from('123')

The array. from method requires that the object meet two conditions:

  • 1. There are 0,1,2,3…… Such as the subscript
  • 2. The length attribute is available

For example:

Concat (arr2)// Merging an array does not change the contents of the original array

Slice (1) // Slice all elements after the second element. Slice does not change the value of the original array. Slice (0) is often used for array copying

Pseudo array:

  • let divArray = document.querySelectorAll('div')
  • There is no array stereotype in the pseudo-array stereotype chain. For example:

3 array add, delete, change, check

3.1 Deleting an Array Element
  • usedeleteDelete elements, for example:
Let arr = new Array(1,2,3); delete arr['0']; // The length of arr is 3Copy the code

An array whose elements are all removed but whose length is not zero is called a sparse array. This method is not recommended.

  • By modifying the arraylengthDelete elements, for example:

However, do not delete array elements this way.

  • Delete the header element

Array.shift () //arr is modified and returns the deleted element

  • Delete the trailing element

Array.pop () // Array is modified and returns the deleted element

  • Delete the middle element
Arr.splice (index,1,'x')// Delete an element starting from index(index,1,'x') and add 'x' arr.splice(index,1,'x','y')// delete, And add 'x','y' to the delete position.Copy the code
3.2 Traversing the number group

\quad We know that we can get the list of key values of an Object by using the object.keys () method and the object.values () method, and also by using the for loop:

for(let key in arr){
   console.log(key)
}
Copy the code

Traversing an object, but since the key values in the object can be set freely, accessing an array in this way is not appropriate. In this case, use:

for(let i = 0; i < arr.length; i++){ console.log(`${i}:${arr[i]}`) }Copy the code

Or, as in Java, use the forEach method of the object:

ForEach (function(x,y){console.log(' ${y}:${x} ') //forEach (function(x,y){console.log(' ${y}:${x} ') //forEach (function(x,y){console.log(' ${y}:${x} ') //forEach (function(x,y){console.log(' ${y}:${x} ') //Copy the code

\quad When you need to find whether an element is in an array, on the one hand, you can iterate through the array. On the other hand, you can use:

Arr.indexof (item) // Returns the index if it exists, otherwise -1Copy the code

Or query the elements that match the criteria:

Arr.findindex (item => item%2 === = 0) arr.findIndex(item => item%2 === 0Copy the code
3.3 Adding an element to an array
  • Add elements to the tail
Arr. Push (newItem) // Modify arr, return length arr. Push (Item1, Item2...) // Modify arr to return the new lengthCopy the code
  • Add the element to the header
Unshift (item1, Item2...) arr. Unshift (item1, Item2...) // Modify arr to return the new lengthCopy the code
  • Add elements in the middle
Arr.splice (index,0,'x') // Insert 'x' arr.splice(index,0,'x','y')Copy the code
3.5 Modifying an element in an Array
  • Reverse order
Arr.reverse () // Reverses the arrayCopy the code
  • Custom order
arr.sort((a,b) => a-b)
Copy the code
3.6 Array transformations (without changing the original array)
  • Map (n to N) Example:

  • Filter (n decreases) Example:

  • Reduce (n change to 1) Example: Finding the sum of array elements

0 is the initial value returned, and each array element is added to sum and the returned value is used as sum for the next iteration. Here’s another example:An empty array is used as the initial value each timereduce, the new return value is passed to the next time as the initial valuereduce.reduceVery powerful, basicallymap.filterIt can be done,reduceYou can do it all. For example, usingreduceFilter elements: