#author("2022-05-05T13:26:11+08:00","default:Admin","Admin")
[[Android]]
#author("2022-05-05T13:27:08+08:00","default:Admin","Admin")
[[ES6]]

&color(red){※前提条件:本情報はAndroid Studio 4.2.2を基づいて説明してる};

#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.childNodes 和方法,如 document.querySelectorAll 返回的。

NodeList 类似于数组,但不是数组,没有 Array 的所有方法,例如find、map、filter 等,但是可以使用 forEach() 来迭代。

#codeprettify{{
const nodeList = document.querySelectorAll(".row");
const nodeArray = [...nodeList];
console.log(nodeList);
console.log(nodeArray);
}}

#hr();
コメント:
#comment_kcaptcha

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS