網易前端微專業,JavaScript程式設計基礎篇:數組

來源:互聯網
上載者:User

網易前端微專業,JavaScript程式設計基礎篇:數組

任何一種語言數組都是比較重要的,其作為一種基礎對象應用很多,如Java你肯定少不了集合(List,Map)這些。因此本篇主要記錄JS的數組使用和常用方法。要點如下:

1,數組建立

兩種方式:

var stu = new Array();

var stu1 = [];
這就和定義對象一樣:

var cat = new Object(); var cat1 = {};
推薦用後者,比較簡潔。如:

var score = [1, 2, 3];
數組裡的東西可以是不同類型的,數組裡面可以是基礎類型也可以是對象或數組:

var array = [ 163, "netease", {color: "red"}, [], true ]; console.log(array[0]); console.log(array[2].color);
再來個:

2,length()函數,得到數組的長度

var stu = [ {id:1, score : 80}, {id:2, score : 75}, {id:3, score : 90}, ]; console.log(stu.length); // 3 stu = []; console.log(stu.length); //0
通過stu[i]訪問並修改第i個元素.
3,indexof()函數,如果能找到返回找到的索引,找不到返回-1,勇於判斷一個元素在不在數組裡

var tel = [101, 110, 139]; var index = tel.indexOf(101); console.log(index);
4,forEach()函數
forEach 需要接受一個回呼函數

var stu = [ {id:1, score : 80}, {id:2, score : 75}, {id:3, score : 90}, ]; var add5 = function(item, number, array){ item.score += 5; }; stu.forEach(add5); console.log(stu[0].score);
這個回調對輸入參數有要求,分別是當前的item,item的索引和整個array.forEach就會自動遍曆每一行,然後將每一行都送給callback函數進行處理。

5,reverse()函數,將數組倒序

var stu = [ {id:1, score : 80}, {id:2, score : 75}, {id:3, score : 90}, ]; stu.reverse(); console.log(stu[0].score);

6,array.sort()函數,該函數傳入一個callback,非常類似java的排序

var stu = [ {id:1, score : 80}, {id:2, score : 75}, {id:3, score : 90}, ]; var bySocre = function(a, b){ return b.score - a.score; } stu.sort(bySocre); var print_callback = function(item, number, array){ console.log(item.score); } stu.forEach(print_callback);
備忘:
a,如果callback裡返回false,則a排在b的前面。如果return的是b - a,則是從大到小排,反之是從小到大排。 
b,sort直接改變了原數組。
c,如果callback不傳,則按照unicode碼自小到大排序:

var names = ["yanzi", "gg", "ww"]; names.sort(); var print_callback = function(item, number, array){ console.log(item); } names.forEach(print_callback);

7,array.push()
在已有數組末尾後面加元素,可以加多個。

8,array.unshift()
在數組的開始位置添加元素。
範例程式碼:

var stu = [ {id:1, score : 80}, {id:2, score : 75}, {id:3, score : 90}, ]; stu.push({id:4, score:100}); stu.unshift({id:5, score:99}); var print_callback = function(item, number, array){ console.log(item.id); } stu.forEach(print_callback);

9,array.shift() 返回第一個元素,同時在原數組裡刪除第一個元素。

10,array.pop() 返回最後一個元素,同時在原數組裡刪除最後一個元素。

11,array.splice(a, b, C)需要傳入三個參數,分別是從位置a開始,刪除b個元素,然後插入元素C,C可以是多個
範例程式碼:

var stu = [ {id:1, score : 80}, {id:2, score : 75}, {id:3, score : 90}, ]; stu.splice(1,1,{id : 5, score:100}); var print_callback = function(item, number, array){ console.log(item.id); } stu.forEach(print_callback);
備忘:如果splice第三個參數不傳入,則只刪除。如果第二個參數傳0,則只插入,不刪除。
總結:reverse,sort, push, unshift, shift, pop, splice都有一個共同特點,都改變了原來的數組。


12,array.slice(start, end)從索引start到end-1拷貝出來一份返回。如果end參數不傳,則截取到最後一個位置。

13,array.concat(a, b):將數組a和數組b串連到一起。

14,array.join(a)對array每個元素用a拼接起來。如果什麼都不傳,預設用,號進行分割。

15,array.map()需傳入一個回調,回調需要return,預設將return的東西push到一個新的array.

var scores = [80, 75, 90]; var addScore = function(item, index, array){ return item + 5; } var scoresNew = scores.map(addScore); var print_callback = function(item, number, array){ console.log(item); } scoresNew.forEach(print_callback);

16,array.reduce()
需要一個callback作為參數,callback(preResult, item, index, array),看一個求和的例子.

var scores = [80, 75, 90]; var sum = function(preresult, item, number, array){ return preresult + item; } var sum2 = scores.reduce(sum, 0); console.log(sum2);

總結:slice, concat, join, map, reduce不會修改原數組。


 

聯繫我們

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