[ES6] array expansion

Extended operator

console.log(...[1, 2, 3])
// 1 2 3

use

1. apply the method of substitution function

function f(x,y,z){}
let args=[1,2,3]
f.apply(null,args)

f(...args)
Math.max(...args)
arr.push(...arr2)

2. Copy array

const arr2=[...arr1] //方法1
const [...arr2]=arr1

3. Combined Array

arr1.concat(arr2,arr3)
[...arr1,...arr2,...arr3]

4. assignment and Deconstruction

const [first, ...rest] = [];
first // undefined
rest  // []

const [first, ...rest] = ["foo"];
first  // "foo"
rest   // []

5. There Iterator interface objects into an array 

Such as strings

[...'hello']
// [ "h", "e", "l", "l", "o" ]

The map, set (de-emphasis)

let map = new Map([
  [1, 'one'],
  [2, 'two'],
  [3, 'three'],
]);

let arr = [...map.keys()]; // [1, 2, 3]

 

Array.from() 

Convert class array of objects and an array of objects of comparable cases

= {arrayLike the let 
    '0': 'A', 
    '. 1': 'B', 
    '2': 'C', 
    length:. 3 
}; 

// the ES5 wording 
var arr1 = [] .slice.call (arrayLike ) ; // [ 'a', 'B', 'C'] 

// wording for ES6 
let arr2 = Array.from (arrayLike); // [ 'a', 'b', 'c']

Extended operator ( ...) may be converted to an array of certain data structures.

Array.fromCan also accept a second parameter, the array acts like mapa method, for each element is processed, the value of the processed returned into the array.

Array.from(arrayLike, x => x * x);
// 等同于
Array.from(arrayLike).map(x => x * x); 

Array.of()

For a set of values, into an array, the main purpose is to compensate for the constructor Array () is less than

Array.of() // []
Array.of(undefined) // [undefined]
Array.of(3, 11, 8) // [3,11,8]
Array.of(3) // [3]
Array.of(3).length // 1

Array() // []
Array(3) // [, , ,]
Array(3, 11, 8) // [3, 11, 8]

Array.ofThe method can be implemented with the following code simulation.

function ArrayOf(){
  return [].slice.call(arguments);
}

Examples of the method copyWithin () 

Examples of Array copyWithin()method, in the current internal array, the member of the specified location copied to other locations (overwrites the existing members), and then returns the current array. That is, using this method, it will modify the current array.

Array.prototype.copyWithin(target, start = 0, end = this.length)
  • target (required): Replace data starting at that position. If negative, it represents the countdown.
  • start (optional): starts to read data from that location, the default is 0. If negative, it represents counted from the end.
  • end (optional): the position to the front stop reading data, by default equal to the array length. If negative, it represents counted from the end.
[1, 2, 3, 4, 5].copyWithin(0, 3)
// [4, 5, 3, 4, 5]

Examples of methods find () and findIndex ()

The first find a qualified member of the

find () returns successfully the member, otherwise undefined

findIndex success back to the member location, otherwise -1

Examples of methods fill ()

Given value, a filled array. The first parameter is the padding value, the second parameter and the second parameter is used to specify the start position and end filling position (not including the far end

[ 'A', 'B', 'C']. Fill (. 7) 
// [. 7,. 7,. 7] 
[ 'A', 'B', 'C']. Fill (. 7,. 1, 2) 
/ / [ 'a', 7, 'c'] 
before the end of the 2nd bit @

Note that if the filling is of type object, then the object is assigned the same memory address, rather than a deep copy of the object .

let arr = new Array(3).fill({name: "Mike"});
arr[0].name = "Ben";
arr
// [{name: "Ben"}, {name: "Ben"}, {name: "Ben"}]

let arr = new Array(3).fill([]);
arr[0].push(5);
arr
// [[5], [5], [5]] 

Examples of the method entries (), keys () and values ​​()

for (let index of ['a', 'b'].keys()) {
  console.log(index);
}
// 0
// 1

for (let elem of ['a', 'b'].values()) {
  console.log(elem);
}
// 'a'
// 'b'

for (let [index, elem] of ['a', 'b'].entries()) {
  console.log(index, elem);
}
// 0 "a"
// 1 "b"

Examples of the method Includes ()

The first parameter is the value you're looking for, is in fact the second location

Successful returns true, false otherwise

Examples of the method flat (), flatMap ()

Leveled list, returns the new array

[1, 2, [3, [4,. 5]]]. Flat () 
// [1, 2, 3, [4,. 5]] No parameter default parameters to 1
 
[1, 2, [3, [4 ,. 5]]]. Flat (2) 
// [. 1, 2,. 3,. 4,. 5] 

[. 1, [2, [. 3]]]. Flat (Infinity) 
// [. 1, 2,. 3] no matter how much nesting leveled with Infinity
 
[. 1, 2,,. 4,. 5] .flat () 
// [. 1, 2,. 4,. 5] skip vacancy

flatMap()The method of performing a function (performed for each member corresponding to the original array Array.prototype.map()), then the array consisting of the implementation of the return value of flat()the method. The method returns a new array does not change the original array. flatMap()Expand the only one array

// equivalent [[[2]], [[. 4]], [[. 6]], [[. 8]]]. Flat () 
[. 1, 2,. 3,. 4] .flatMap (X => [[ 2 * X]]) 
// [[2], [. 4], [. 6], [. 8]]

Vacancy array

A set position index no value

Array(3) // [, , ,]

Note that the space is not undefined, the value is equal to a position undefined代表still has value. Vacancy is no value.

  • forEach()filter()reduce()every() And some()will skip gap.
  • map()Skip vacancy, but will retain this value
  • join()And toString()it will be considered a gap undefined, and undefinedand nullare treated as an empty string

ES6 is explicitly into space undefined.

Array.from () into the spaceundefined

Space into an extended operation methodundefined

copyWithin()Copy will be connected with the gap.

fill()Vacancy will be considered normal array position.

for...ofCycle will traverse space.

entries(), keys(), values(), find()And findIndex()it will be treated as a gap undefined.

Array.prototype.sort () sorted stability

Sorting stability (stable sorting) is an important attribute sorting algorithms, refer to the same sort key projects, before and after the sort order unchanged.

  

Guess you like

Origin www.cnblogs.com/Mijiujs/p/12111642.html