es6數組必看太實用了


隨着前后分離,前端人員也要寫大量的邏輯代碼,es5很多地方尤其是數據工具大拿數組,很多時候都是捉襟見肘。

繼而,es6為我們擴展了很多good的工具和方法,讓我們一起學習es6吧。

1原型方法from

遵循規則有點苛刻

let  json = {
    '0': 'websong',
    '1': '17歲',
    '2': 'web',
    length:3 //值得注意的是,key值必須是數字,並且最后還要有這個length
}
let arr=Array.from(json);
console.log(arr)
//我相信這個會在以后的版本里升級的吧,這種語法太雞肋了。

2.find或者filter我認為沒啥區別

let arr2=[1,2,3,4,5,6,7,8,9];
let arr2_new=arr2.find(function (vlue,i,t) {
    return vlue>5;
});//實例方法--查找
console.log(arr2_new);

3。fll替換,這讓我想到了字符串中的增刪查改

let arr=['websong','web','17歲'];
arr.fill('webStorm',1,2);//替換 很有意思,1=要替換的元素下標,2如果寫0或者1,都沒效果,所以我任務2是長度。如果寫3它會把后面的17歲頁替換
document.write(arr);

4.數組里的循環

有數組必有循環啊

強大的for of循環

//常規的
for(let itme of arr){
    console.log(itme)
}
//我只要key
for(let itme of arr.keys()){
    console.log(itme)
}
//我要鍵值成對兒的,
for(let itme of arr.entries()){
    console.log(itme)
}
//鍵值由我控制,想要就要不要就不要
 for(let [index,val] of arr.entries()){
    console.log(index+':'+val)
}

  有時候我們拿到后台返回的數組可能格式很復雜,也就是說它不規則

entries()幫我們很好的解決了這個問題----手動循環

let list=arr.entries();//如果是不規則的數組,就哦那個entries條目寫法。手動循環。
console.log('**********')
console.log(list.next().value);
console.log('*******************');
console.log(list.next().value);
console.log('*******************');
console.log(list.next().value);

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM