What is an array

An array is a special kind of object, a classification of objects.

JS arrays are not typical arrays

Typical array
  1. Elements have the same data type
  2. Use continuous memory storage
  3. Gets the element by numeric subscript

JS array
  1. The data type of the element can be different
  2. Memory is not necessarily contiguous (objects are stored randomly)
  3. You can’t subscript numbers, you can subscript strings
  4. Arrays can have any key.

Create an array

new

let arr = [1.2.3]
let arr = new Array(1.2.3)
Copy the code

conversion

let arr = '1, 2, 3'.split(', ')
let arr = '123'.split(' ')
Array.from('123')	// Try to convert something that is not an array into an array
Copy the code

When you have subscripts and thetalengthProperty, can become an object, andlengthDetermine the length.

Pseudo array

An array that has no common array property is called a pseudo-array.

let divList = document.querySelectorAll('div')
Copy the code

Merge array

arr1.concat(arr2)
Copy the code

Intercepts part of an array

arr1.slice(1)	// Start from the number of elements, 1 represents the second element
arr1.slice(0)	// All of them
let arr2 = arr1.slice(0)	/ / copy
Copy the code

Add, delete, change and check array elements

Deletion is not recommended

1. Delete objects in the same way

delete arr[1]	// The length remains the same, and the deleted elements become empty
Copy the code

  1. By modifying thelenghtTo delete elements

Remove elements correctly

  1. Delete header elements
arr.shift()	// The arR is modified and returns the deleted element
Copy the code

  1. Delete the tail element
arr.pop()	// The arR is modified and returns the deleted element
Copy the code

  1. Delete intermediate elements
arr.splice(2, n)	// Delete n elements after subscript position 2
arr.splice(2, n, 'x')	// Add the x element to the delete position (multiple elements can be added)
Copy the code

View all elements

  1. Query the attribute name and value of a number (string)
for(let i = 0; i < arr.length; i++){
    console.log(`${i}: ${arr[i]}`)
}
arr.forEach(function(item, index){
	console.log(`${index}: ${item}`)})ForEach () executes the given function once on each element of the array
Copy the code

Viewing individual elements

  1. Like an object

  1. Find if an element is in an array
arr.indexOf(item)	// If the index exists, return -1 if it does not
Copy the code

  1. Use criteria to find elements or indexes
arr.find(item= > item % 2= = =0)	// Find the first multiple of 2
arr.findIndex(item= > item % 2= = =0)	// Find the epitome of the first multiple of 2
Copy the code

Increments an element in an array

  1. Add elements to the tail
arr.push(newItem)
arr.push(item1,item2)
Copy the code
  1. Add elements to the header
arr.unshift(newItem)
arr.unshift(item1,item2)
Copy the code
  1. Add elements in the middle
arr.unshift(index, 0.'x')// Insert x at index, 0 means no deletion
Copy the code

Changing the order of elements

  1. Reverse order
arr.reverse()// Modify the original array
Copy the code

  1. Custom order
arr.sort((a, b) = > a - b)//
Copy the code

An array of conversion

arr.map()	N/a/n
arr.filtr()	/ / n
// Do not change the original array
Copy the code

reduce

let arr = [1.2.3.4.5.6]
//undefined
arr.reduce((sum, item) = > {return sum + item}, 0)
//0 is the initial value of sum. The final value of sum is sum+item1+item2.
/ / 21
arr.reduce((result, item) = > {return result.concat(item * item)}, [])
//[] is the initial value of result
//(6) [1, 4, 9, 16, 25, 36]
arr.reduce((result, item) = > result.concat(item % 2= = =1 ? [] : item), [])
// If item/2=1, it returns an empty array, responsible for returning item
/ / (3) [2, 4, 6]
Copy the code

Code practice

arr.reduce((result, item) = > {
    if(item.parent === null){
       result.id = item.id
       result['name'] = item['name']}else{
       result.children.push(item)
       delete item.parent
       item.children = null
    }
    return result
}, {id: null.children: [])	// Initialize the array children}
//{id: 1, children: Array(2), name: "animals "}
//children: Array(2)
//0: {name: "dog ", id: 2, children: null}
//1: {name: "cat ", id: 3, children: null}
//length: 2
//__proto__: Array(0)
//id: 1
// Name: "animal"
//__proto__: Object
Copy the code