...可以將類陣列物件迅速轉換成陣列
window.onload=function(){
let aLi = document.querySelectorAll('ul li');
let arrLi = [...aLi];
arrLi.pop();//刪除一項
arrLi.push('aasdf');//末尾增加一項
console.log(arrLi);//[li,li,li,aasdf]
}
複製程式碼
Array.from()可以將類陣列物件迅速轉換成陣列
類陣列:通常具有length
let arr=[1,2,3];
let arr2=[...arr];
let arr3 = Array.from(arr);//[1,2,3]
複製程式碼
window.onload=function(){
let aLi = document.querySelectorAll('ul li');
let arrLi = Array.from(aLi);//ES6
let arrLi = [].slice.call(aLi);//ES5
arrLi.pop();
arrLi.push('aasdf');
console.log(arrLi);
}
複製程式碼
function show(){
let args=Array.from(arguments);
console.log(args);
};
show(1,2,3,4,5);
複製程式碼
let str= 'strive';
// let arr=str.split('');
let arr = Array.from(str);//把類陣列變成陣列
console.log(arr);
複製程式碼
Array.of可以將一組值轉換成陣列
let arr=Array.of('apple','banana','orange');//把一組值轉化成陣列
console.log(arr);
複製程式碼
arr.find查詢第一個符合條件的值,如果沒有找到,返回undefined
let arr1=[2,4,7,1,6];
let res=arr1.find((val,index,arr)=>{
return val>5;
});
console.log(res);//7
複製程式碼
arr.findIndex查詢第一個符合條件的選項,返回索引值,沒找到返回-1
let arr2=[2,4,7,1,6];
let ress=arr2.findIndex((val,index,arr)=>{
return val>5;
});
console.log(ress);//2
複製程式碼
arr,fill(填充的東西,開始位置,結束位置)
let arr3 = new Array(10);
arr3.fill('預設值');
console.log(arr3);//(10) ["預設值", "預設值", "預設值", "預設值", "預設值", "預設值", "預設值", "預設值", "預設值", "預設值"]
let arr4=arr3.fill('haha',1,4);
console.log(arr4);//(10) ["預設值", "haha", "haha", "haha", "預設值", "預設值", "預設值", "預設值", "預設值", "預設值"]
複製程式碼
ES2016裡新增arr.includes
let arr5 = ['apple','banana','orange','strawberry'];
let b=arr5.includes('orange222');
console.log(b);//false
複製程式碼