1. 程式人生 > >網易前端微專業,JavaScript程序設計基礎篇:數組

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

start 數組創建 重要 dsc splice 程序 () tro eve

不論什麽一種語言數組都是比較重要的,其作為一種基礎對象應用非常多,如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不會改動原數組。


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