ES6 的数组知识

// json数组格式
let json={
    // key:value
    '0':'nl',
    '1':'jun',
    '2':'ming',
    // length可见是json数组格式
    length:3
}
// 使用转换成数组的方法是Array.from()方法
let arr=Array.from(json);
console.log(arr);

//这里看下Array.of方法的使用
let arr1=Array.of(3,4,5,6);
let arr2=Array.of('nl','jun');
console.log(arr1);
console.log(arr2);

//find()实例方法(因为需要有了实例之后才能有这个方法来使用),用于查找数字或者字符串
let arr3=[1,2,3,4,5,6,7,8,9];
console.log(arr3.find(function(value,index,arr3){
    return value>5;
    // 如果返回undefined说明没有存在这样的值
}));

 

//fill
let arr=['nl','jun','ming'];
//1-3,替换掉了jun和ming
arr.fill('web',1,3);
console.log(arr);

//for of循环,比for in直观
let arr1=['mudan','juhua','guihua'];
for(let item of arr1){
    console.log(item);
}
// 下面是输出下标
for(let item of arr1.keys()){
    console.log(item);
}
// 下面是输出索引和值
for(let item of arr1.entries()){
    console.log(item);
}
//但是我们要用数组把值提炼出来,才是我们想要的
for(let [index,val] of arr1.entries()){
    console.log(index+':'+val);
}

// 这里说一下es6到es5的转换,我们可以借用工具babel或者gulp

// 下面讲一下entries实例方法,entries()实例方式生成的是Iterator形式的数组
let list=arr1.entries();
console.log(list);
// 下面工作中常用的手动循环(用next()手动跳转到下一个值)
console.log(list.next().value);
console.log('---------------------------');
console.log(list.next().value);
console.log('***************************');
console.log(list.next().value);
console.log('5555555555555555555555555555');

 

posted on 2018-09-14 16:59  云点西西  阅读(135)  评论(0编辑  收藏  举报

导航