數組在咱們前端開發中扮演着舉足輕重的角色,全面而熟悉的瞭解數組各類方法的使用,對於開發可謂是事半功倍,下面進入正題。前端
JavaScript中建立數組有兩種方式:
數組
var arr = new Array();//建立一個空數組var arr = new Array(20);//建立一個包含20 項的數組var arr = new Array('Lily','Lucy','Tom');//建立一個包含三個字符的數組複製代碼
var arr = [];//建立一個空數組var arr = [10];//建立一個包含一項的數組複製代碼
var arr = [1,2,3,4,5];var arr1 = arr.join();var arr2 = arr.join(',');var arr3 = arr.join('-');var arr4 = arr.join(':');console.log(arr); //[1, 2, 3, 4, 5]console.log(arr1);//1,2,3,4,5console.log(arr2);//1,2,3,4,5console.log(arr3);//1-2-3-4-5console.log(arr4);//1:2:3:4:5複製代碼
var arr = [1,2,3,4];
var arr1 = arr.push(5);
var arr2 = arr.push(6,7);
var arr3 = arr.push([8,9]);
var arr4 = arr.push([10,11],[12]);
console.log(arr); //[1,2,3,4,5,6,7,[8,9],[10,11],[12]]
console.log(arr1);//5
console.log(arr2);//7
console.log(arr3);//8
console.log(arr4);//10複製代碼
var arr = [1,2,3,4,5];
var a = arr.pop();
console.log(arr);// [1,2,3,4];
console.log(a); // 5;複製代碼
var arr = [1,2,3,4,5];
var a = arr.shift();
console.log(arr);// [2,3,4,5];
console.log(a); // 1複製代碼
var arr = [4,5,6,7];
var a = arr.unshift(2);
console.log(arr);// [2,4,5,6,7]
console.log(a);// 5
var arr = [3,4,5,6];var a = arr.unshift(1,2);console.log(arr);//[1,2,3,4,5,5];console.log(a); //6複製代碼
var arr = [3,4,5,6,7,8];var a = arr.reverse();
console.log(arr);[8,7,6,5,4,3]console.log(a);//[8,7,6,5,4,3]複製代碼
var arr = [3,4,5];
var a = arr.concat(6);//
console.log(a);//[3,4,5,6]
var b = arr.concat([7,8]);
console.log(b);//[3,4,5,7,8]
var c = arr.concat(9,[10,11]);
console.log(c); //[3,4,5,9,10,11]
var d = arr.concat([6,[7,8]]);
console.log(d);//[3,4,5,6,[7,8]]
console.log(arr);//[3,4,5];複製代碼
var arr = [3,4,5,6];
var a = arr.slice(1);
console.log(arr);//[3,4,5,6]
console.log(a);//[4,5,6]
var b = arr.slice(1,3)
console.log(arr);//[3,4,5,6]
console.log(b);//[4,5]
var c = arr.slice(1,-1);
console.log(c);//[4,5]
var d = arr.slice(-3,-1)
console.log(d);//[4,5]複製代碼
一個參數:
var arr = [2,3,4,5];
var a = arr.splice(2);
console.log(arr);//[2,3]
console.log(a);//[4,5]
兩個參數:
var arr = [3,4,5,6,7,8];
var b = arr.splice(1,3);
console.log(arr);//[3,7,8]
console.log(b);//[4,5,6]
複製代碼
var arr = [2,3,4,5,6,7,8,9];
var c = arr.splice(3,0,12,34,56);
console.log(arr);[2, 3, 4, 12, 34, 56, 5, 6, 7, 8, 9]
console.log(c);//[]複製代碼
var arr = [2,3,4,5,6];
var d = arr.splice(2,2,12,33);
console.log(arr);[2, 3, 12, 33, 6]
console.log(d);[4,5]複製代碼
indexOf(): 查找給定元素在數組中位置的索引值。
var arr = [1,3,5,7,7,5,3,1];
console.log(arr.indexOf(5));//2
console.log(arr.lastIndexOf(5));//5
console.log(arr.indexOf(3,3));//6 從索引3 開始查找到第一個
console.log(arr.indexOf(10));//-1
console.log(arr.indexOf('10'));//-1複製代碼
var arr = [2,3,4,5,6,7];
arr.forEach(function(item, index, array){//item表明每一項,index當前項的索引,array 當前數組;} )複製代碼
var arr = [1,2,3,4,5,6,7];
var arr2 = arr.map(function(item) {return item * item;})
console.log(arr);//[1,2,3,4,5,6,7];
console.log(arr2);//[1, 4, 9, 16, 25, 36, 49];複製代碼
var arr = [1,2,3,4,5,6];
var b = arr.filter(function(item,index, array) {//item爲每一項的值,index爲每一項的索引,array就是arr.return item % 2 == 0;})
console.log(arr);//[1,2,3,4,5,6]
console.log(b);//[2,4,6]複製代碼
var arr = [1,2,3,4,5,6,7];
var a = arr.every(function(item) {return item < 10;})
console.log(arr);//[1,2,3,4,5,6,7]
console.log(a);//true複製代碼
var arr = [2,3,4,5,6];
var b = arr.some(function(x) { return x > 4;})
console.log(arr);// [2,3,4,5,6];
console.log(b);//true複製代碼
var arr = [1,2,3,4,5,6,7];
var b = arr.reduceRight((prev,cur, index,array) => { return prev + cur;})
console.log(arr);//[1,2,3,4,5,6,7]
console.log(b);//28複製代碼