您的位置:首页 > 其它

Web端 es6(基础五) 数组扩展

2018-02-03 13:29 363 查看
常用
转换为数字 Arrayof

对每个元素进行操作

fill 批量替换

数组遍历

数组浅复制 copyWithin

数组位置查找 find

数组过滤

常用

转换为数字 Array.of

let numbers = Array.of(1,2,3,4);
console.
4000
log(numbers)
// 输出结果
(4) [1, 2, 3, 4]


对每个元素进行操作

//对每个元素进行操作
let array = Array.from([1,2,3],(item) =>{
return item * 2
})
console.log(array)
// 输出结果
(3) [2, 4, 6]


fill 批量替换

fill (替换的数值,起始位置,结束位置)

console.log([1,'hello',undefined].fill(7))
console.log(['a','b','c','d','e','f','g'].fill(7,1,3))
// 输出结果
(3) [7, 7, 7]
(7) ["a", 7, 7, "d", "e", "f", "g"]


数组遍历

for (let index of ['a','b','c']){
console.log(index)
}
// 输出结果 abc

for (let index of ['a','b','c'].keys()){
console.log(index)
}
// 输出结果  0 1 2

for (let [index,value] of ['a','b','c'].entries()){
console.log(index,value)
}
// 输出结果
0 "a"
1 "b"
2 "c"


数组浅复制 copyWithin

方法浅复制数组的一部分到同一数组中的另一个位置,并返回它,而不修改其大小。

console.log([33,38,39,40,50].copyWithin(0,3,4))
// 输出结果
(5) [40, 38, 39, 40, 50]


数组位置查找 find

// 输出查找 值
let result = [1,2,3,4,5,6].find((item)=>{
return item > 3;
})
console.log(result)
// 输出结果 4

// 查找下标
let result = [1,2,3,4,5,6].findIndex((item)=>{
return item > 3;
})
console.log(`index=${result}`)
// 输出结果
index = 3


数组过滤

let result = [1,2,3,4,5,6].filter((item)=>{
return item > 3;
})
console.log(result)

// 输出结果
(3) [4, 5, 6]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  fliter es6 数组