當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
网易前端微专业,JavaScript程序设计基础篇:数组
生活随笔
收集整理的這篇文章主要介紹了
网易前端微专业,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不會改動原數組。
總結
以上是生活随笔為你收集整理的网易前端微专业,JavaScript程序设计基础篇:数组的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 外媒:欧盟禁止政府人员在官方设备上安装T
- 下一篇: Spring思维导图(IOC篇)