Array.from 類數組,Set,字符串轉為數組
Array.of 不定參數轉為數組
Array.prototype.fill(value,[start],[end]) 對數組在指定范圍填充值
Array.prototype.find() 查找滿足條件的首個元素,否則返回undefined
Array.prototype.findIndex() 查找滿足條件的首個元素的索引Array.prototype.includes() 判斷數組是否包含指定的值,包含返回true,否則返回false,對象不能檢測
Array.prototype.entries() 用數組的索引和值組成的對象生成一個迭代器
Array.prototype.keys() 用數組的索引生成一個迭代器
Array.prototype.values() 用數組的值生成一個迭代器
String.prototype.trim() 去除兩端空格
String.prototype.repeat() 重復字符串生成新字符串
Sting.prototype.startsWith(str,position) 以指定子字符串開始,開始位置可選
String.prototype.endsWith(str,position)以指定子字符串結尾,結束位置可選
String.prototype.includes(str,positon) 是否包含指定的子串,位置可選
Object.assign 對象合並
1. range函數實現
1.1 keys方法
function range(start,end){ return [...Array(end - start).keys()].map(item => start + item); }
在簡單情形下,可以直接
[...Array(5).keys()] 來生成一個0到4的序列
1.2 fill方法
function range(start,end){ return Array(end-start).fill(0).map((el,i) => start + i); }
1.3 Array.from方法
function range(end) { return Array.from({ length: end }, (_, index) => index); }
2. 數組去重
function unique(array) { return Array.from(new Set(array)); }
function unique(array) { return [...new Set(array)]; }