JavaScript 進階(未完待續)

來源:互聯網
上載者:User

標籤:刪除   array   返回   start   刪除元素   rip   header   rom   ...   

集合Array
  1. 2種建立數組的方式

    var fruits = [] ;
    var friuits = new Array();

  2. 遍曆

    fruits.forEach(function (item, index, array){
    console.log(item, index);
    });
    // Apple 0
    // Banana 1

  3. 基本操作

操作 代碼 傳回值
添加元素到數組的末尾 fruits.push(‘Orange‘) 數組長度
添加元素到數組的頭部 fruits.unshift(‘Strawberry‘) 數組長度
刪除頭部元素 fruits.shift(); 頭部元素
刪除尾部元素 fruits.pop(); 尾部元素
找出某個元素在數組中的索引 fruits.indexOf(‘Banana‘); 下標
通過索引刪除某個元素 fruits.splice(index, 1); 被刪除的元素
複製數組 var shallowCopy = fruits.slice(0,length); 返回指定範圍內元素組成的新數組
產生數組 Array.from() Array.from() 方法從一個類似數組或可迭代對象中建立一個新的數組執行個體。
  1. 根據索引刪除元素的例子

    /* splice(start: number, deleteCount: number, ...items: T[]): T[]; */

    var fruits = ["apple","b","c","d"] ;
    console.log("array is : ");
    fruits.forEach(function (item, index, array){
    console.log(item, index);
    });
    var index = fruits.indexOf("b");
    fruits.splice(index,1);
    console.log("array is : ");
    fruits.forEach(function (item, index, array){
    console.log(item, index);
    });

  2. Array.from()使用例子

    var fruits = ["apple","b","c","d"] ;
    var f = Array.from(fruits);
    f.forEach(function (item, index, array){
    console.log(item, index);
    });
    //apple 0
    //b 1
    //c 2
    //d 3

    var f = Array.from("hello");
    f.forEach(function (item, index, array){
    console.log(item, index);
    });
    //h
    //e
    //l
    //l
    //o

    Array.from()還可以用於Set,Map

SetMap

JavaScript 進階(未完待續)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.