Array Awesome JS

array SUMMARY

此文乃对JS数组的总结

Basic grammar

Array

syntax

literal : [ele0,ele1]  
new Array(ele0,ele1,eleN)  
new Array(length)  

Access array elements
differ length and numerical

properties

  • Array.length
  • Array.protorype
    • Allows the addition of properties to all array objects.

Methods

  • Array.from() ES6 Creates a new Array instance from an array-like or iterable object.
// Examples
Array.from([1, 2, 3], x => x + x);  
// [2, 4, 6]
Array.from({length: 5}, (v, k) => k);  
// [0, 1, 2, 3, 4]
  • Array.isArray() Returns true if a variable is an array, if not false.

  • Array.observe() Non-standard Asynchronously observes changes to Arrays, similar to Object.observe() for objects. It provides a stream of changes in order of occurrence.

  • Array.of() Creates a new Array instance with a variable number of arguments, regardless of number or type of the arguments.

Array Instances

  • Array.prototype.constructor Specifies the function that creates an object's prototype.
  • Array.prototype.length Reflects the number of elements in an array.

Methods

对于方法来说,需要的是:

  1. 是否改变原数组
  2. 调用方法后返回的是什么
Mutator methods 存取器方法

These methods modify the array : 注意返回的值,并且原数组都被修改。

  • Array.prototype.copyWithin() copyWithin() 方法用于在数组内的替换操作,即替换元素与被替换元素都是数组内的元素。ES6标准
  • Array.prototype.fill()
[1, 2, 3].fill(4);               // [4, 4, 4]
[1, 2, 3].fill(4, 1);            // [1, 4, 4]
[1, 2, 3].fill(4, 1, 2);         // [1, 4, 3]
  • pop() Removes the last element from an array and returns that element.
  • push() Adds one or more elements to the end of an array and returns the new length of the array.
  • reverse() Reverses the order of the elements of an array in place — the first becomes the last, and the last becomes the first.
  • shift() Removes the first element from an array and returns that element.
  • unshift() Adds one or more elements to the front of an array and returns the new length of the array.
  • sort() Sorts the elements of an array in place and returns the array.
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
  • splice() The splice() method changes the content of an array by removing existing elements and/or adding new elements.
Accessor methods

These methods do not modify the array and return some representation of the array. 不改变原数组

Array.prototype.xx()

  • concat() Returns a new array comprised of this array joined with other array(s) and/or value(s).
  • includes()
[1, 2, 3].includes(2);     // true
[1, 2, 3].includes(4);     // false
  • join() The join() method joins all elements of an array into a string.
  • slice() The slice() method returns a shallow copy of a portion of an array into a new array object.
  • toSource()
  • toString()
  • toLocaleString()
  • indexOf()
  • lastIndexOf()
Iteration methods
  • forEach()
//迭代数组,执行回调函数
arr.forEach(callback[, thisArg])  
callback: currentValue index array  
  • entries() ES6
  • every() Returns true if every element in this array satisfies the provided testing function.
  • some() Returns true if at least one element in this array satisfies the provided testing function.
  • filter() Creates a new array with all of the elements of this array for which the provided filtering function returns true.
  • find() The find() method returns a value in the array, if an element in the array satisfies the provided testing function. Otherwise undefined is returned.
  • findIndex() The findIndex() method returns an index in the array, if an element in the array satisfies the provided testing function. Otherwise -1 is returned.
  • keys() Iterator . ES6 迭代器接口
  • map() Creates a new array with the results of calling a provided function on every element in this array.
  • reduce() The reduce() method applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value.
  • reduceRight()
  • values() : not supported by broswers