学习笔记:ES6之数组扩展(重要)

来源:互联网 发布:java继承实现接口用 编辑:程序博客网 时间:2024/06/11 05:19

数组新增特性:

Array.from(内容)

Array.from() 方法从一个类似数组或可迭代的对象中创建一个新的数组实例。

Array.of()--将一串数字转换成数组

Let arr=Array.of(3,4,5,6,7);

console.log('arr=',arr);

--arr=[3, 4, 5, 6, 7]

letempty=Array.of();

console.log('empty=',empty);

--empty= []

copyWithin(起始位置,读取数据位置,截止位置之前读取数据)

console.log([1,2,3,4,5].copyWithin(0,3,4));--[4,2,3,4,5]

find()/findIndex()

console.log([1,2,3,4,5,6].find(function(item){

return item >3;--- 4 (只返回第一个满足条件的内容)

}))

console.log([1,2,3,4,5,6].findIndex(function(item){

return item>3;---3  返回第一个满足条件的下标

}))

 

fill(填充内容,起始位置,结束位置)--填充数组

console.log('fill-1',[1,'a',undefined].fill(7));-- 7 7 7

console.log('fill,pos',[1,'a',undefined].fill(7,1,3));--fill,pos [1, 7, 7]

entries()/keys()/values()--遍历

for(letindexof['1','c','ks'].keys()){

console.log('keys',index);---012

}

for(letvalueof['1','c','ks'].values()){

console.log('values',value);---1 c ks

}

for(let[index,value]of['1','c','ks'].entries()){

console.log('index->values',index,value);

}---

index->values 0 1

index->values 1 c

index->values 2ks

includes()--判断是否包含

console.log('number',[1,2,NaN].includes(1));--true

原创粉丝点击