An array of API

A static method

Array.from()

Array.from(arrayLike[, mapFn[, thisArg]])

  • arrayLike

    • A pseudo-array object or iterable that you want to convert to an array.
  • MapFn optional

    • If specified, the callback is performed for each element in the new array.
  • ThisArg optional

    • This object when the callback function mapFn is executed.

Return value: a new array instance

Array.isArray()

Array.of()

Parameters: an Array of (for element0 [, element1 elementN [,] [,…]])

Return value: a new array instance

Members of the method

concat()

Parameter: array or parameter list

const NewArray = Array1.concat(array2)
const NewArray = Array.concat(1.2.3.4)
Copy the code

Return value: a new array instance

Function: Merge arrays can also be used for shallow copies

// The shallow copy copies only the outermost layer
const NewArray = Array1.concat()
Copy the code

every()

Every (callback(element[, index[, array]])[, thisArg])

  • callback

    A function that tests each element. It takes three arguments:

    • element

      The current value for the test.

    • The index of the optional

      Index of the current value used for the test.

    • An array of optional

      Call the current array of every.

Return value: True if each return of the callback is a Truthy value, false otherwise.

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

some()

With every

Return: true if one passes

fill()

Arr. fill(value[, start[, end]])

  • value

    The values used to populate the elements of the array.

  • The start of the optional

    Start index, default is 0.

  • The end of the optional

    Terminates the index, which defaults to this.length.

Return value: modified array

// Can be used to initialize an array
Array(3).fill(0);   / / [0, 0, 0]
Copy the code

filter()

Var newArray = arr.filter(callback(element[, index[, array]])[, thisArg])

Return value: a new array of elements that passed the test, or an empty array if no array elements passed the test.

[1.2.3.4.5].filter(x= > x > 1) // [2, 3, 4, 5]
[1.2.3.4.5].filter(x= > x > 6) / / []
Copy the code

find()

Parameter: arr.find(callback[, thisArg])

Return value: the value of the first element in the array that satisfies the provided test function, otherwise undefined is returned.

[1.2.3.4.5].find(x= > x > 6) //undefined
[1.2.3.4.5].find(x= > x > 2) / / 3
Copy the code

findIndex()

The difference from find is the return value

Array by providing the index of the first element of the test function. Otherwise, return -1.

flat()

Var newArray = arr.flat([depth])

  • Depth: The default depth is 1

Return value: a new array instance.

flatMap()

flatMap() = map() + flat(1)

forEach()

Executes the given function once for each element of the array

Callback (currentValue [, index [, array]])[, thisArg])

Return value: there is no return value undefined

map()

The effect differs from forEach() in that map() has a return value

Return value: a new array consisting of the result of executing the callback function for each element of the original array

includes()

Return value: Boolean

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

indexOf()

Parameter: arr.indexOf(searchElement[, fromIndex])

  • FromIndex (optional)

    • Specifies where to start the search

Return value: find the index index of the element or -1 if it cannot be found

var array = [2.5.9];
array.indexOf(2);     / / 0
array.indexOf(7);     // -1
array.indexOf(9.2);  / / 2
array.indexOf(2, -1); // -1
array.indexOf(2, -3); / / 0
Copy the code

join()

var a = ['Wind'.'Rain'.'Fire'];
var myVar1 = a.join();      // change myVar1 to "Wind,Rain,Fire"
var myVar2 = a.join(', ');  // change myVar2 to "Wind, Rain, Fire"
var myVar3 = a.join('+'); // change myVar3 to "Wind + Rain + Fire"
var myVar4 = a.join(' ');    // myVar4 changes to "WindRainFire"
Copy the code

reduce()

Callback (callback(Accumulator, currentValue[, index[, array]]) {

  • The initialValue: initial value

Return value: the result of the cumulative processing of the function

[0.1.2.3.4].reduce((prev, curr) = > prev + curr ) / / 10
[0.1.2.3.4].reduce(((prev, curr),10) = > prev + curr , 10); // the initial value of 20 is 10
Copy the code

reverse()

Return value: inverted array (modifies the original array directly)

slice()

Arr. Slice ([begin[, end]])

If omitted, it starts at 0 by default

Return value: a new array instance (without modifying the original array)

Function: slice array/shallow copy/convert to real array

Array.prototype.slice.call(arguments)
Copy the code

sort()

Parameters: the arr. Sort ([compareFunction])

var numbers = [4.2.5.1.3]; 
numbers.sort((a, b) = > a - b); / / ascending
numbers.sort((a, b) = > b - a); / / descending
Copy the code

Return value: sorted array (modified array)

splice()

Arguments: the array splice (start [, deleteCount [, item1 [, item2 [,…]]]])

  • DeleteCount (optional)

    Integer representing the number of array elements to remove.

    • >0 Delete the element

    • <0 Add element

Add/Delete

  • Pop () deletes the last element and returns the deleted element

  • Push () adds an element at the end of the array and returns the length of the new array

  • Shift () removes the first element and returns the deleted element

  • Unshift () adds an element to the beginning of the array and returns the length of the new array


Sort an array

/ / ascending
arr.sort((x,y) = > x-y)
/ / descending
arr.sort((x,y) = > y-x)
Copy the code

Array to heavy

Implement a

With the ES6 Set feature there are no duplicate elements

Realize the

You sort the array and then you compare the adjacent elements and if they’re the same then you move on to the next one

Implement three

Double for loop

To compare

The specific code is as follows

function getArr(arr) {
  const timer = 10000
  for (let i = 0; i < timer; i++) {
    arr[i] = Math.floor(Math.random() * 100)}return arr
}

let arr1 = []
let arr2 = []
let arr3 = []

// Generate three arrays of length timer
arr1 = getArr(arr1)
arr2 = getArr(arr2)
arr3 = getArr(arr3)


// case 1
console.time('case1')
arr = [...new Set(arr1)]
console.timeEnd('case1')

// case 2
console.time('case2')
arr2 = arr2.sort((x, y) = > x - y)
const res2 = []
for (let i = 0; i < arr2.length; i++) {
  if(arr2[i] ! == arr2[i +1]) {
    res2.push(arr2[i])
  }
}
console.timeEnd('case2')

// case3
console.time('case3')
const res3 = []
for (let i = 0; i < arr3.length; i++) {
  for (let j = i + 1; j < arr3.length; j++) {
    if (arr3[i] == arr3[j]) {
      break
    }
    if (j == arr3.length - 1) {
      res3.push(arr3[i])
    }
  }
}
console.timeEnd('case3')

// output
/ / case1:0.467 ms
/ / case2:5.254 ms
/ / case3:11.034 ms
Copy the code

Therefore, the Set of ES6 is the most efficient method

An array of random sequence

implementation

Take the subscripts in the length range of two arrays and switch positions

var arr = [1.2.3.4.5.6.7.8]

function random(arr) {
  const timer = 1000
  var x, y, temp
  for (let i = 0; i < timer; i++) {
    x = Math.floor(Math.random() * arr.length)
    y = Math.floor(Math.random() * arr.length)
    temp = arr[x]
    arr[x] = arr[y]
    arr[y] = temp
    / / ES6 writing
    / /; [arr[x], arr[y]] = [arr[y], arr[x]]
  }
  return arr
}
Copy the code

Array flattening

Implement a

Call Array. Prototype. Flat

Realize the

If they’re all numbers you can use toString to convert them to strings and split them

Implement three

Recursive calls

var arr = [1[1.2.3].4.5]
var res = []
function flat(arr) {
  // case 1
  // return arr.flat()

  // case 2
  // return arr
  // .toString()
  // .split(',')
  // .map((item) => +item)

  // case 3
  // for (var i = 0; i < arr.length; i++) {
  // if (arr[i] instanceof Array) {
  // flat(arr[i])
  // } else {
  // res.push(arr[i])
  / /}
  // }

  // return res
}
Copy the code