Method added on constructor
-
Array.of(), which makes up for the fact that an Array is passed a parameter.
console.log(Array()); // -> [] console.log(Array.of()); Console.log (Array(3)); // -> [] // Array(); // -> [empty × 3] console.log(array.of (3)); // -> [3] console.log(Array(1, 2)); // -> [1, 2] console.log(Array.of(1, 2)); / / - > [1, 2]Copy the code
-
Array.from(), which converts array-like conversions and objects deployed with the Iterator interface to arrays. The first argument is arrayLike, the object to be converted to an array, the second optional argument, mapFn, to process the converted array twice, and the third optional argument to change the “this” pointer. The latter two parameters are not normally used.
function foo (a, b, c, d) { console.log(arguments); ƒ : ƒ, Symbol(Symbol. Iterator): ƒ] Arguments = Array. console.log(arguments); // -> [1, 2, 3, 4] } foo(1, 2, 3, 4); let obj = { start: [1, 2, 3], end: [4, 5], [Symbol.iterator] () { let index = 0, arr = [...this.start, ...this.end], len = arr.length; return { next () { if(index < len){ return {value: arr[index++], done: false} }else{ return {value: undefined, done: true} } } } } } console.log(Array.from(obj)); // -> [1, 2, 3, 4, 5]Copy the code
Methods added to the prototype
-
[]. Fill (value, start, end), fill the array (change the original array), the first parameter is specified to fill the value, the last two parameters are not specified, default to fill the entire array. The second argument contains the current index for the index started (default: 0), and the third argument does not contain the current index for the index ended (default: this.length).
let arr = [1, 2, 3, 4]; arr.fill(5); console.log(arr); // -> [5, 5, 5, 5] arr.fill(5, 1); console.log(arr); // -> [1, 5, 5, 5] arr.fill(5, 1, 2); console.log(arr); Let obj = {length: 3}; let obj = {length: 3}; [].fill.call(obj, 4); console.log(obj); // -> {0: 4, 1: 4, 2: 4, length: 3}Copy the code
-
[].keys()/values()/entries(), the return value is an iterator, and for of can be used to iterate through the current corresponding value.
-
Note: Unlike object.keys ()/values()/entries(), methods on the Object constructor return an array, whereas here we return an iterator.
let arr = ['a', 'b', 'c']; console.log(arr.keys()); // -> Array Iterator {} console.log(arr.values()); // -> Array Iterator {} console.log(arr.entries()); // -> Array Iterator {} let iter = arr.values(); console.log(iter.next()); // -> {value: 'a', done: false} console.log(iter.next()); // -> {value: 'b', done: false} console.log(iter.next()); // -> {value: 'c', done: false} console.log(iter.next()); // -> {value: undefined, done: true} for(let item of arr.entries()){ console.log(item); // -> [0, 'a'] [1, 'b'] [2, 'c'] } Copy the code
-
[].find()/findIndex() returns the index of the first member that meets the condition of the callback function.
let arr = [1, 2, 3, 4]; let res = arr.find((value, index, arr) => { return value > 2; }) console.log(res); // -> 3 let idx = arr.findIndex(value => value > 2); console.log(idx); // -> 2 // fixed indexOf() bug console.log([NaN].indexof (NaN)); // -> -1 console.log([NaN].findIndex(val => Object.is(NaN, val))); / / - > 0Copy the code
-
[].includes(), which queries whether the array contains the parameters passed in, returns a Boolean value.
console.log([1, 2, 4, NaN].includes(3)); // -> false console.log([1, 2, 4, NaN].includes(NaN)); // -> true Copy the code