ES6

ES6学习-数组

2018-03-13  本文已影响7人  YINdevelop

1.将一堆文本或者变量转换成数组

格式:Array.of(文本)

let arr =Array.of(3,4,5,6);
console.log(arr);    //[3, 4, 5, 6]

2.数组内成员的替换

格式:数组.fill(要替换的内容,开始替换的index值,结束替换的index值)

注:包含开始index,不包含结束index

let arr=[0,1,2,3,4,5,6,7,8,9];
arr.fill('js',2,5);
console.log(arr); //[0, 1, "js", "js", "js", 5, 6, 7, 8, 9]

3.数组遍历

1.格式:for..of

es5写法

let arr=['test0','test1','test2']
for (let x in arr){
    console.log(x,arr[x]);
}
 // 0,test0
    1,test1
    2,test2

es6输出数组成员

let arr=['test0','test1','test2']
for (let x of arr){
    console.log(x);
}
// test0
   test1
   test2    

es6输出index(arr.keys())

let arr=['test0','test1','test2']
for (let y of arr.keys()){
    console.log(y);
}
//0 1 2

es6同时输出数组成员和index(arr.entries())

let arr=['test1','test2','test3']
for (let [index,val] of arr.entries()){
    console.log(index,val);
}

2.格式:arr.forEach

let arr=['js','es6'];
arr.forEach((val,index)=>console.log(val,index)); 

//js,0
  es6,1

3.格式:arr.filter

let arr=['js','es6'];

arr.filter((val,index)=>console.log(val,index));

//js,0
  es6,1

4.格式:arr.some

let arr=['js','es6'];

arr.some((val,index)=>console.log(val,index));

//js,0
  es6,1

5.格式:arr.map

let arr=['js','es6'];

arr.map((val,index)=>console.log(val,index));

//js,0
  es6,1
上一篇下一篇

猜你喜欢

热点阅读