fill( )实例方法:
fill()也是一个实例方法,它的作用是把数组进行填充,它接收三个参数,第一个参数是填充的变量,第二个是开始填充的位置,第三个是
填充到的位置。
1
2
3
|
let
arr
=
[
0
,
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
]
;
arr
.
fill
(
'jspang'
,
2
,
5
)
; //[0, 1, "jspang", "jspang", "jspang", 5, 6, 7, 8, 9]
console
.
log
(
arr
)
;
|
数组的遍历
for…of循环:
let arr = ['judy','nihao','dajia']
for ( let index of arr.keys() ){
console.log(index); //输出序列号 0 1 2
}
let arr=['judy','nihao','web'];
for (let item of arr){
console.log(item); //judy nihao web
}
//同时输出数组的内容和索引
let arr = ['judy','nihao','dajia'];
for (let [index,val] of arr.entries()){
console.log(index+':'+val);
}
entries()
entries()实例方式生成的是Iterator形式的数组,那这种形式的好处就是可以让我们在需要时用next()手动跳转到下一个值。我们来看下面的代码:
//entries( )实例方法:
let arr = ['judy','nihao','dajia'];
let list=arr.entries();
console.log(list.next().value);
console.log(list.next().value);
console.log(list.next().value);