Home > Article > Web Front-end > What are the new array methods in es6?
es6 array methods include: 1. Array.from(), used to convert array-like objects or traversable objects into real arrays; 2. Array.of(), used to convert a set of values, Convert to an array; 3. copyWithin(), used to copy members at the specified position to other positions within the current array; 4. fill(); 5. find(); 6. findIndex(); 7. includes() ; 8. entries(); 9. keys(); 10. values().
The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.
Modify the original array | Do not modify the original array |
---|---|
push, pop | concat |
unshift, shift | join |
sort | slice |
indexOf(), lastIndexOf() | |
forEach | |
map | |
filter | |
##some | |
every | |
reduce,reduceRight | |
includes | |
finde, findIndex | |
entries(), keys(), values() |
Array method
Array.from()is used to combine two types of objects Convert to a real array: array-like object and iterable object (including ES6's new data structures Set and Map).
let arrayLike = { '0': 'a', '1': 'b', '2': 'c', length: 3 }; let arr2 = Array.from(arrayLike); // ['a', 'b', 'c']
Array.from can also accept a second parameter, which is similar to the map method of an array. It is used to process each element and put the processed value into the returned array.
Array.from(arrayLike, x => x * x); // 等同于 Array.from(arrayLike).map(x => x * x); Array.from([1, 2, 3], (x) => x * x) // [1, 4, 9]Array.of()
is used to convert a set of values into an array.
Array.of(3, 11, 8) // [3,11,8] Array.of(3) // [3]
Instance method
will change the original array
array. copyWithin(target, start = 0, end = this.length);target (required): The position from which to start replacing data. If it is a negative value, it represents the reciprocal value.
// 将3号位复制到0号位 [1, 2, 3, 4, 5].copyWithin(0, 3, 4) // [4, 2, 3, 4, 5] // -2相当于3号位,-1相当于4号位 [1, 2, 3, 4, 5].copyWithin(0, -2, -1) // [4, 2, 3, 4, 5]
['a', 'b', 'c'].fill(7); // [7, 7, 7] let arr = new Array(3).fill([]); arr[0].push(5); // [[5], [5], [5]]Will not change the original array
[1, 4, -5, 10].find((n) => n < 0) // -5 [1, 5, 10, 15].find(function(value, index, arr) { return value > 9; }) // 10
[1, 5, 10, 15].findIndex(function(value, index, arr) { return value > 9; }) // 2
[1, 2, 3].includes(2) // true
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"
If you do not use a for...of loop, you can manually call the next method of the traverser object to traverse.
let letter = ['a', 'b', 'c']; let entries = letter.entries(); console.log(entries.next().value); // [0, 'a'] console.log(entries.next().value); // [1, 'b'] console.log(entries.next().value); // [2, 'c']
[Recommended learning:
javascript advanced tutorialThe above is the detailed content of What are the new array methods in es6?. For more information, please follow other related articles on the PHP Chinese website!