Create an array

  1. new Array
  2. Array
  3. literal
  4. Array.from()[ES6] -> [ES6]
  5. Array.of() [parameter] -> [array] [ES6]

An array of attributes

Length Array length

Detection array

  1. instanceof Array
  2. Array.isArray()

Through the array

for(let i = 0; i < nums.length; i++) {
	// nums[i]
}
Copy the code
for(i in nums){
	// nums[i]
}
Copy the code
nums.forEach((item) = >{
	// item
})
Copy the code
for(item of nums){
	// item
}
Copy the code

Conversion method

  1. toString()[comma-separated string]
  2. toLocaleString()
  3. valueOf()[Array itself]

Stack method [change the original array]

  1. push()Return the length of the new array
  2. pop()[Top of stack] popup [Return deleted item]

Queue method change the original array

  1. Push () [end of queue] join queue

  2. Shift () [queue head] [return deleted item]

  3. Unshift () [queue head] join queue

  4. Pop () [end of queue] [return deleted item]

Sorting method 【 change the original array 】

  1. reverse()Return call array reference
  2. sort()Sort an array of numbers from smallest to largestsort((a,b)=>a-b)]

Do not change the original array

  1. concat()Concatenate array, flatten array of parameters return newly constructed array, do not change the original array
  2. slice([begin[, end]])Return the newly constructed array without changing the original array.
  3. splice(start[, deleteCount[, item1[, item2[, ...]]]])Delete (start position, number of deleted) Insert (start position, 0, element to be inserted) Replace (start position, number of deleted, element to be inserted)
  4. flat([depth])Flat array [ES10] [Return the newly constructed array without changing the original array]
const arr = [0.1.2[3.4]].const newArr = arr.flat();
console.log(newArr); // [0, 1, 2, 3, 4]
console.log(arr); // [0, 1, 2, [3, 4]]

const arr2 = [0[1.2], [3[4.5]]];
const newArr2 = arr2.flat(1);
console.log(newArr2); // [0, 1, 2, 3, [4, 5]]
Copy the code

Merge method [do not change the original array]

  1. reduce()
  2. reduceRight()

【JS】JavaScript array – merge method -reduceRight

25 Advanced Uses of Array Reduce that you Need to know

let values = [1.2.3.4.5]
let sum = values.reduce((prev, cur, index, array) = > prev + cur)
console.log(sum) / / 15
Copy the code

Search and position methods

  1. IndexOf (Elements to be searched, [initial search location])[Return position]
  2. lastIndexOf() [Return position]
  3. includes() 【ES7】
  4. find()Return the first matched element.
  5. findIndex()Return the index of the first matched element.
const evens = [2.4.6]
let result = evens.find((element, index, array) = > element === 4) 
let result2 = evens.findIndex((element, index, array) = > element === 4) 
console.log(result) / / 4
console.log(result2) / / 1
Copy the code

Iterative method [do not change the original array]

  1. every()[Return true if each item is true]
  2. some() Return true if any item is true.
  3. filter()[Returns array of true items]
  4. forEach()[No return value]
  5. map()[Returns a new array of results for each run]

【ES6】JavaScript array – iterator method -keys()-values()-entries()- iterator method -every()-some()-filter()-map()-forEach()

const evens = [2.4.6]
let result = evens.every((element, index, array) = > element % 2= = =0) 
console.log(result) // true
Copy the code

Iterator method [ES6]

  1. keys()
  2. values()
  3. entries()

【ES6】JavaScript array – iterator method -keys()-values()-entries()- iterator method -every()-some()-filter()-map()-forEach()

Copy fill method 【 change the original array 】【ES6】

  1. CopyWith (target[, start[, end]]) [Copy part of array to another location in the same array]

  2. Fill (target[, start[, end]]) [fixed value to fill an array]

const arr = [1.2.3.4.5.6.7.8.9];
arr.copyWithin(0.3.6);
console.log(arr); //[4, 5, 6, 4, 5, 6, 7, 8, 9]

const arr1 = [1.2.3.4.5.6.7.8.9];
const a = arr1.fill(0.1.5);
console.log(arr1); // [1, 0, 0, 0, 0, 6, 7, 8, 9]
console.log(a); // [1, 0, 0, 0, 0, 6, 7, 8, 9]
Copy the code

Extended operator.【 ES6 】

const colors = ['green'.'red'.'pink'];
const colors1 = ['white'.'grey'];
const colors2 = [...colors, ...colors1];
console.log(colors2); // [ 'green', 'red', 'pink', 'white', 'grey' ]
Copy the code