Article From:


Array.ofUsed to convert a set of values into an array.

  let arr = Array.of(3,4,7,9,11);
  console.log(arr);    //[3, 4, 7, 9, 11]

  let empty=Array.of();
  console.log('empty',empty);    //[]

Only when the number of parameters is not less than 2,Array()A new array of parameters is returned. The number of parameters is only one, which is actually the length of the specified array.

Array(3) // [, , ,]

Array.of()The generated array is not ergodic (Iterable).


Array.fromThe method is used to turn two classes of objects into a real array: an array – like object (array-like object) and an object that traversable (Iterable) (including the ES6 new data structure Set and Map).

The following is an array of objects,Array.fromTurn it into a real array.

let arrayLike = {
    '0': 'a',
    '1': 'b',
    '2': 'c',
    length: 3

// ES5The method of writing
var arr1 = []; // ['a', 'b', 'c']

// ES6The method of writing
let arr2 = Array.from(arrayLike); // ['a', 'b', 'c']

Array.fromYou can also accept second parameters that act like arrays.mapMethod is used to process each element and put the processed value into the returned array.

Array.from([1, 2, 3], (x) => x * x)
// [1, 4, 9]

3. array instance fill ()”

fillThe method fills an array with a given value.

  console.log([1,'a',undefined].fill(7)); //[7, 7, 7]
  console.log(['a','b','c','d','e'].fill(7,1,3));   //["a", 7, 7, "d", "e"]
//From position 1 to position 3 end

  for(let index of ['1','c','ks'].keys()){
//keys 0
// keys 1
// keys 2
  for(let value of ['1','c','ks'].values()){
//values 1
// values c
// values ks
  for(let [index,value] of ['1','c','ks'].entries()){
//0 "1"
// 1 "c"
// 2 "ks"

5. array instance copyWithin ()”

Array instancecopyWithinMethod, within the current array, copy the members of the specified location to other locations (override the original member), and then return to the current array. That is to say, using this method will modify the current array.

// [4, 2, 3, 4, 5]
//Position 0 is replaced, and the replacement value ends from position 3 to position 4.}

6. array instance find () and findIndex ()”.

Array instancefindMethod, used to find outThe first conforms to the conditionsA member of an array. Its parameter is a callback function, and all the array members execute the callback function sequentially until the first return value is found.trueA member and then return to the member. If there is no qualified member, returnundefined

  console.log([1,2,3,4,5,6].find(function(item){return item>3}));     //4
  console.log([1,2,3,4,5,6].findIndex(function(item){return item>3}));     //3

7. array instance includes ()”

Method returns a Boolean value indicating whether an array contains a given value and a string.includesThe method is similar. This method is introduced by ES2016.

  console.log([1,2,NaN].includes(1));   //true
  console.log([1,2,NaN].includes(NaN));     //true

includeYou can find NaN, find can’t

Link of this Article: ES6 array extension