一.Array
console.log(Array());//[]
console.log(Array(3));//[empty*3]
console.log(Array(3,1));//[3, 1];
console.log(new Array());[]
console.log(new Array(3))//[empty*3]
console.log(Array.of());//[]
console.log(Array.of());//[3]
console.log(Array.of(3, 4, 6));//[3, 4, 6]
二.ArrayForm
var oList = document.querySelectorAll('p');
console.log([].slice.call(oList))//[p,p,p]
console.log(Array.from(oList));//[p,p,p]
let obj = {
start: [12,3,4,5],
end: [7,8,9],
[Symbol.iterator](){
let index = 0,
arr = [...this.start,...this.end],
len = arr.length;
return {
next(){
if(index<len){
return {
value: arr[index++],
done: false
}
}else{
return {
value: undefined,
done: true
}
}
}
}
}
}
log(Array.from(obj))//[12,3,4,5,7,8,9]
三.数组拓展
(1).map
let arr = [1, 2, 3, 4]
let arr1 = arr.map(function(value){
return value*2
})
console.log(arr1);//返回新数组 可以循环数组执行对应函数
2 4 6 8
(2).Array.form
console.log(Array.from(obj,function(val,idx){
return val * 2
}))//[24, 6, 8, 10, 14, 16, 18]
(3).fill
通过Array.of()声明的数组可以通过fill来填充数据
默认start = 0,如果不指定则从0开始全部替换了,且操作的是原数组
let arr1 = [1, 2, 3, 4];
var arr = arr1.fill(5);
console.log(arr1);//[5,5,5,5]
arr1.fill(5, 1)//[1,5,5,5] 左闭右开
arr1.fill(5,1,2)//[1,5,3,4]
arr1.fill(5,1,1)//[1,2,3,4]
arr1.fill(5,-3,-2)//[1,5,3,4]
arr1.fill(5,NaN,-2)//[5,5,5,5]数字不合法当0处理,两个数组不合法直接不处理
cosole.log([].fill.call({length:3},4));
//{
0:4,
...
2:4
length:3
}
(4).keys/values/entries
let arr = ['a', 'b', 'c'];
console.log(arr.keys()); //Array Iterator{}
console.log(arr.values());//Array Iterator{}
console.log(arr.entries());//Array Interator{}
var iter = arr.entries();
console.log(iter.next());
console.log(iter.next());
console.log(iter.next());
console.log(iter.next());
{
value:[0,'a']
done:false
}
{
value:[1,'b']
done:false
}
{
value:[2,'c']
done:false
}
{
value:undefined
done:true
}
即代表我可用for of循环取值 keys entries一致不做演示
for(let i of iter){
console.log(i);
}// a b c
(5).copywithin
var arr = [1,2,3,4,5,6,7]
console.log(arr.copyWithin(2));//[1,2,1,2,3,4,5]
console.log(arr.copyWithin(-2));//[1,2,3,4,5,1,2]
第一个参数确定位置,第二个第三个位置确定要复制的值,结果不改变数组长度
var arr = [1,2,3,4,5,6,7];
console.log(arr.copyWithin(0,3))//[4,5,6,7,5,6,7]
console.log(arr.copyWithin(0,3,4))//[4,2,3,4,5,6,7]
console.log(arr.copyWithin(-2,-3,-1))//[1,2,3,4,5,5,6]
console.log([].copyWithin.call({length:5,3:1},0,3),0,3);
{
0:1
3:1
length:5
}
console.log([].copyWithin.call({length:5,3:1,4:1,5:1},0,3));
{
0:1
1:1
3:1
4:1
5:1
length:5
}
超过长度了也会复制过去和length无关
console.log([].copyWithin.call({length:5,3:1,4:1,5:1,6:1},0,3))
{
0:1
1:1
3:1
4:1
5:1
6:1
length:5
}
(6).find/findex
//基本形式
var arr = [1,2,3,4];
var arr1=arr.find(function(value,index,arr){
return value>2
})
console.log(arr1);//3
var arr1=arr.find(function(value,index,arr){
return index>2
})
console.log(arr1);//4
访问没有的值
var arr = [1,2,3,4];
var value = arr.find(function(value,index,arr){
return value >5
})
console.log(index);//undefined
var index = arr.findIndex(function(value,index,arr){
return value >2
})
console.log(index);//2
访问没有的值
var arr = [1,2,3,4];
var value = arr.findIndex(function(value,index,arr){
return value >5
})
console.log(index);//-1
查询不合法数字
es5
console.log([NaN].indexOf(NaN));//-1
es6
console.log([NaN].findIndex(y => Object.is(NaN,y)))//0
(7).includes
console.log([1,2,3].includes(1));//true
console.log([1,2,NaN].includes(NaN));true
四.数值拓展
//es5进制表达方式
console.log(Number.prototype.toString.call(503,2))//11111011 2进制
console.log(parseInt(111110111,2))//503 2进制
//es6进制表达方式
console.log(0x1f7)//16进制数表达
console.log(0o767)//8进制
console.log(0b11110111)//2进制
部分方法从全局挪到了Number上,这样就不会隐式转换,更加安全
console.log(isNaN("NaN"));true
console.log(Number.isNaN("NaN"))false
console.log(isFinite(42)); // true有限数
console.log(isFinite(Infinity))//false 无限数
console.log(isFinite('42'))//true 会隐式转换
console.log(Number.isFinite('42'))//false 修正了
parseFloat和parseInt也挪动到了Number上
console.log(Number.isInteger(24.0)) //true
console.log(Number.isInteger(24.1)) //false
MAX_SAFE_INTEGER: 9007199254740991
console.log(Number.MAX_SAFE_INTEGER === Math.pow(2,53)-1);//true
MIN_SAFE_INTEGER: -9007199254740991
console.log(Number.MAX_SAFE_INTEGER === -Math.pow(2,53)+1);//true
log(Number.isSafeInteger(1.1)) false