一、静态方法
(一)、Array.from(arg)
1、用法
用于将两类对象转为真正的数组:类似数组的对象(array-like object)和可遍历(iterable)的对象。
<script>
const divs = document.getElementsByTagName("div"); // 类数组
const result = Array.from(divs);
console.log(result);
</script>
(二)、Array.of(…args)
1、用法
使用指定的数组项创建一个新数组;
<script>
var arr = Array.of(1,2,3,4,5);
console.log(arr); // [1, 2, 3, 4, 5]
</script>
二、实例方法
(一)、find(callback)
1、用法
用于查找满足条件的第一个元素,没找到得到undefined
(二)、findIndex(callback)
1、用法
用于查找满足条件的第一个元素的下标, 没找到返回-1
<script>
const Stu = [{ sname: "chen", id: 2001},
{ sname: "li", id: 2002},
{ sname: "zhang", id: 2003},
{ sname: "yang", id: 2004},
{ sname: "yu", id: 2005},
{ sname: "shi", id: 2006},
{ sname: "liu", id: 2007}]
const result = Stu.find(item => item.id === 2001) // 找Stu中某一项的id值等于2001的哪一项返回
const resultIndex = Stu.findIndex(item => item.id === 2000)//没有找到返回-1
console.log(result, resultIndex);
</script>
(三)、fill(data)
1、用法
用指定的数据填充满数组所有的内容
<script>
// 创建了一个长度为10的数组,数组的每一项是"1"
const arr = new Array(10);
arr.fill("1");
console.log(arr)
</script>
(四)、copyWithin(target, [start], [end])
1、用法
在数组内部完成复制,参数1:从下标几开始改变数据;参数2:从哪一位开始复制数据,默认是第一位; 参数3:在那个位置停止复制数据
<script>
const arr = [1, 2, 3, 4, 5, 6];
arr.copyWithin(2, 1, 3); // [1, 2, 2, 3, 5, 6]
console.log(arr)
</script>
ES7 中的数组API
includes(data):判断数组中是否包含某个值,使用Object.is匹配
<script>
const arr = [1,3,5,4,2];
console.log(arr.indexOf(3); //true
console.log(arr.includes(9)); // flase
</script>