Home  >  Article  >  Web Front-end  >  Es6 array extension

Es6 array extension

php中世界最好的语言
php中世界最好的语言Original
2018-03-19 16:41:511463browse

This time I will bring you the expansion of Es6 array. What are the precautions for Es6 array expansion? Here is a practical case, let’s take a look.

Extension of array
1. ExtensionOperator: You can convert the array into Single parameter separated by comma
...[1,2,3] //Console operation error
console.log(...[1,2,3]);//1, 2,3
(1)Replace the apply method
function test(a,b){return a+b;}
test.apply(null,[1,2]) Same function as test(...[1,2])
(2) Copy array
var arr1 = [1,2,3],var arr2 = [];
arr2 = arr1.concat();
arr2 = [...arr1] or [...arr2] = arr1 //The effect of copying the array can also be achieved
Changing the value of arr2 will not affect arr1
(3) Merge arrays
Append arr2 to the end of arr1
var arr1 = [1,2,3], arr2 = [4,5,6],arr3 ;
Array.prototype.push.apply(arr1,arr2); Same as arr1.push(...[arr2]);
arr3 = [...arr1,...arr2] // [1,2,3,4,5,6]
(4) Combined with destructuring assignment to assign value
[a,...b] = [1,2,3, 4,5] // a---1, b---->[2,3,4,5]
(5)Convert string to array
let str = "word";
console.log(...str);//['w','o','r','d']
can also be recognized Unicode encoding exceeds \uFFFF and requires four bytes to represent characters var str = "asdc

The above is the detailed content of Es6 array extension. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn