扩展运算符
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
|
ログイン
]
開始行:
[[ES6]]
#contents
用三个点号表示,功能是把数组或类数组对象展开成一系列用逗...
#codeprettify{{
console.log(...[1,2,3]);//1 2 3
let arr = ['blue', 'pink', 'green'];
...arr // blue, pink, green
console.log(...arr); // blue pink green
}}
* 合并数组 [#i7cfe818]
#codeprettify{{
let arr1 = ['blue', 'pink', 'green'];
let arr2 = [1, 2, 3];
let arr3 = [...arr2, ...arr1];
arr1.push(...arr2);
console.log(arr1); // ["blue", "pink", "green", 1, 2, 3]
console.log(arr3); //[1, 2, 3, "blue", "pink", "green"]
}}
* 参数传递 [#ld9868a0]
从下面的代码看,函数定义了多少个参数,扩展运算符传入的值...
#codeprettify{{
const sum = (num1, num2) => num1 + num2;
console.log(sum(...[6, 7])); // 13
console.log(sum(...[6, 7, 8])); // 13
}}
* 数组去重 [#sf2af5c3]
与 Set 一起使用消除数组的重复项,如下:
#codeprettify{{
const arrayNumbers = [1, 5, 9, 3, 5, 7, 10, 4, 5, 2, 5];
const newNumbers = [...new Set(arrayNumbers)];
console.log(newNumbers); // [ 1, 5, 9, 3, 7, 10, 4, 2 ]
}}
* 字符串转字符数组 [#l3e8fc5f]
#codeprettify{{
const title = "china";
const charts = [...title];
console.log(charts); // [ 'c', 'h', 'i', 'n', 'a' ]
//进而可以简单进行字符串截取,如下:
const title = "china";
const short = [...title];
short.length = 2;
console.log(short.join("")); // ch
}}
* NodeList 转数组 [#e192ccf0]
NodeList 对象是节点的集合,通常是由属性,如 Node.childNod...
NodeList 类似于数组,但不是数组,没有 Array 的所有方法,...
#codeprettify{{
const nodeList = document.querySelectorAll(".row");
const nodeArray = [...nodeList];
console.log(nodeList);
console.log(nodeArray);
}}
#hr();
コメント:
#comment_kcaptcha
終了行:
[[ES6]]
#contents
用三个点号表示,功能是把数组或类数组对象展开成一系列用逗...
#codeprettify{{
console.log(...[1,2,3]);//1 2 3
let arr = ['blue', 'pink', 'green'];
...arr // blue, pink, green
console.log(...arr); // blue pink green
}}
* 合并数组 [#i7cfe818]
#codeprettify{{
let arr1 = ['blue', 'pink', 'green'];
let arr2 = [1, 2, 3];
let arr3 = [...arr2, ...arr1];
arr1.push(...arr2);
console.log(arr1); // ["blue", "pink", "green", 1, 2, 3]
console.log(arr3); //[1, 2, 3, "blue", "pink", "green"]
}}
* 参数传递 [#ld9868a0]
从下面的代码看,函数定义了多少个参数,扩展运算符传入的值...
#codeprettify{{
const sum = (num1, num2) => num1 + num2;
console.log(sum(...[6, 7])); // 13
console.log(sum(...[6, 7, 8])); // 13
}}
* 数组去重 [#sf2af5c3]
与 Set 一起使用消除数组的重复项,如下:
#codeprettify{{
const arrayNumbers = [1, 5, 9, 3, 5, 7, 10, 4, 5, 2, 5];
const newNumbers = [...new Set(arrayNumbers)];
console.log(newNumbers); // [ 1, 5, 9, 3, 7, 10, 4, 2 ]
}}
* 字符串转字符数组 [#l3e8fc5f]
#codeprettify{{
const title = "china";
const charts = [...title];
console.log(charts); // [ 'c', 'h', 'i', 'n', 'a' ]
//进而可以简单进行字符串截取,如下:
const title = "china";
const short = [...title];
short.length = 2;
console.log(short.join("")); // ch
}}
* NodeList 转数组 [#e192ccf0]
NodeList 对象是节点的集合,通常是由属性,如 Node.childNod...
NodeList 类似于数组,但不是数组,没有 Array 的所有方法,...
#codeprettify{{
const nodeList = document.querySelectorAll(".row");
const nodeArray = [...nodeList];
console.log(nodeList);
console.log(nodeArray);
}}
#hr();
コメント:
#comment_kcaptcha
ページ名: