es6 文法 (數組擴充)

來源:互聯網
上載者:User

標籤:reac   開始   數組   foreach   文法   相容問題   log   第一個   empty   

{    let arr = Array.of(3, 4, 7, 9, 11);    console.log(‘arr‘, arr); //[3,4,7,9,11]    let empty = Array.of();    console.log(empty); //[]}//ES6為Array增加了from函數用來將其他對象轉換成數組。//當然,其他對象也是有要求,也不是所有的,可以將兩種對象轉換成數組。//1.部署了Iterator介面的對象,比如:Set,Map,Array。//2.類數組對象,什麼叫類數組對象,就是一個對象必須有length屬性,沒有length,轉出來的就是空數組。{    //轉換數組    let p = document.querySelectorAll(‘p‘);    let pArr = Array.from(p);    pArr.forEach(function(item) {        console.log(item.textContent); //es6,嘿嘿嘿,快樂動起來    });    //map    console.log(Array.from([1, 3, 5], function(item) {            return item * 2        })) //2,6,10}//數組填充{    console.log(‘fill-7‘, [1, ‘a‘, undefined].fill(7)) // 7,7,7    console.log(‘fll,pos‘, [‘a‘, ‘b‘, ‘c‘].fill(‘7‘, 1, 3)); //a 7 7 (1,3)表示起始位置,7為填充內容}//遍曆相關{    //取位置    for (let index of[‘1‘, ‘c‘, ‘ks‘].keys()) {        console.log(‘keys‘, index); //0 1 2    }    //取值    for (let value of[‘1‘, ‘c‘, ‘ks‘].values()) {        console.log(‘values‘, value); //1 c ks (這裡有相容問題,需引入 import ‘babel-polyfill‘)    }    //取位置和值    for (let [index, value] of[‘1‘, ‘c‘, ‘ks‘].entries()) {        console.log(‘values‘, index, value); //0 1 2,1 c ks    }}//在當前數組內部數組內一個成員賦值到其他地方{    console.log([1, 2, 3, 4, 5].copyWithin(0, 3, 4)); //4,2,3,4,5 (開始位置0,從第三個開始讀起,截止位置為4,即5個數)    console.log([1, 2, 3, 4, 5].copyWithin(1, 3, 4)) //1,4,3,4,5    console.log([1, 2, 3, 4, 5].copyWithin(0, -2, -1)) //4,2,3,4,5}//尋找{    console.log([1, 2, 3, 4, 5, 6].find(function(item) {        return item > 3 //4 只找第一個值,不往後了    }));    console.log([1, 2, 3, 4, 5, 6].findIndex(function(item) {        return item > 3 //3 找的是下標    }));}//NaN {    console.log(‘number‘,[1,2,NaN].includes(1)); // true 是否包含這個值    console.log(‘number‘,[1,2,NaN].includes(NaN)); // true}

 

es6 文法 (數組擴充)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.