Array.of() 创建数组
let arr=Array.of(3,4,5);
console.log(arr);//[3, 4, 5]
let empty=Array.of();
console.log(empty);//[]
Array.from() 把伪数组或者集合转换成数组
var aLi=document.getElementsByTagName('li');
console.log(aLi instanceof Array);//false
var rLi=Array.from(aLi);
console.log(rLi instanceof Array);//true
var result=Array.from([1,3,5],function(item){
return item*2;
})
console.log(result);//[2, 6, 10]
fill() 用指定的元素填充数组 ,返回填充后的数组
var arr=[1,undefined,null,NaN];
var result=arr.fill('fill');
console.log(result);//["fill", "fill", "fill", "fill"]
还可以接受另外两个参数,代表替换的开始位置和结束位置(开区间),第三个参数可省略,代表替换到结尾
var arr=[1,undefined,null,NaN];
var result=arr.fill('fill',1,3);
console.log(result);//[1, "fill", "fill", NaN]
var arr=[1,undefined,null,NaN];
var result=arr.fill('fill',1);
console.log(result);//[1, "fill", "fill", "fill"]
遍历相关的
keys()
var arr=[1,2,3];
for(let key of arr.keys()){
console.log(key);//0 1 2
}
values() chrome不兼容
let arr=[1,2,3];
for(let value of arr.values()){
console.log(value);//1 2 3
}
entries()
let arr=[1,2,3];
for(let [index,value] of arr.entries()){
console.log(index+':'+value);
//0:1
//1:2
//2:3
}
let arr1=['a','b','c','d','e'];
console.log(arr.copyWithin(0,3,4));//["d", "b", "c", "d", "e"]
console.log(arr);//["d", "b", "c", "d", "e"]
let arr2=['1','2','3','4','5'];
console.log(arr2.copyWithin(1,2,4));//["1", "3", "4", "4", "5"]
findIndex()找出第一个满足条件的元素,返回这个元素的索引位置
let arr=[1,2,3,4,5,6];
let result=arr.find(function(item){return item>3;})
console.log(result);//3
let index=arr.findIndex(function(item){return item>3;});
console.log(index);//3
includes() 判断数组是否包含某个元素
let arr=['a',null,undefined,NaN,-0];
console.log(arr.includes(NaN));//true
console.log(arr.includes(null));//true
console.log(arr.includes(undefined));//true
console.log(arr.includes('b'));//false
console.log(arr.includes(0));//true