1. 程式人生 > >JavaScript 之 陣列常用方法總結

JavaScript 之 陣列常用方法總結

陣列中的常用方法總結:

能改變原陣列的方法有:push,  pop,  shift,  unshift,  sort,  reverse,   splice

不能改變原陣列的方法有:concat,  join,  split,  toString,  slice

改變原陣列方法:

1.push

作用:像陣列的末尾新增一項或多項元素
引數:要新增的項
返回值:新陣列的長度
是否改變原陣列:改變
var ary = ['a','b','c'];
var res = ary.push('d','e');
console.log(ary);  // ["a", "b", "c", "d", "e"]
console.log(res);  // 5

2.pop

作用:刪除陣列的最後一項
引數:無
返回值:被刪除的項
是否改變原陣列:改變
var ary = ['1','2','3'];
var res = ary.pop();
console.log(ary);  // ['1','2']
console.log(res);  // 3

3.shift

作用:刪除陣列的首項
引數:無
返回值:被刪除的項
是否改變原陣列:改變
var ary = ['a','b','c'];
var res = ary.shift();
console.log(ary);  // ['b','c']
console.log(res);  // a

4.unshift

作用:向陣列的開頭新增一或多項
引數:要新增的項,多項用','隔開
返回值:新陣列的長度
是否改變原陣列:改變
var ary = ['a','b','c'];
var res = ary.unshift('d','e');
console.log(ary);  // ["d", "e", "a", "b", "c"]
console.log(res);  // 5

5.splice

作用:增刪改
引數:ary.splice(index,howmany,item1,.....,itemX)
返回值:刪除的項
是否改變原陣列:改變

增加的功能
ary.splice(n,0,x,......,y);

從陣列的索引n開始,刪除0項,在索引n的後邊增加新的項,第三個引數開始都是用來填補刪除的專案位置的

var ary = [1,2,3,4,5];
var res = ary.splice(1,0,6,7);
console.log(ary);  // [1, 6, 7, 2, 3, 4, 5]
console.log(res);  // [] 刪除0項,返回一個空陣列
刪除的功能
ary.splice(n,m);

從陣列的索引n開始,刪除m項

var ary = [1,2,3,4,5];
var res = ary.splice(1,2);
console.log(ary);  // [1,4,5]
console.log(res);  // [2,3]

修改的功能
ary.splice(n,m,x);

從陣列的索引n開始,刪除m項,把x新增到索引n前邊

var ary = [1,2,3,4,5];
var res = ary.splice(1,2,6,7);
console.log(ary);  // [1, 6, 7, 4, 5]
console.log(res);  // [2,3]
//模擬 push(尾部新增)  和push二者返回值不同
ary.splice(ary.length,0,新的項) //因為splice是在索引前新增,所以第一個引數為ary.length
//模擬 pop(尾部刪除)
ary.splice(arr.length-1,1);
//模擬 shift(首項刪除)
ary.splice(0,1)
//模擬 unshift(首項新增) 和unshilft二者返回值不同
ary.splice(0,0,新的項)

此外:

ary.splice(n)  // 表示從索引n開始刪除到末尾
ary.splice(0)  // 刪除整個陣列 有克隆陣列的效果,利用返回值

6.sort

作用:對陣列的元素進行排序 
引數:可選(函式) 規定排序規則 預設排序順序為按字母升序
返回值:排好序的原陣列
是否改變原陣列:改變
var ary = [1,5,7,9,12,24,56,87,92];
var ary2 = [1,5,7,9,12,24,56,87,92];
var ary3 = [1,5,7,9,12,24,56,87,92];
var res = ary.sort();
var res2 = ary2.sort(function(a,b){
    return a-b;   //升序
})
var res3 = ary3.sort(function(a,b){
    return b-a;   //降序
})
// sort的引數函式總的形參a,b就是陣列排序時候的相鄰比較的兩項
console.log(res);  // [1, 12, 24, 5, 56, 7, 87, 9, 92]
console.log(res2); // [1, 5, 7, 9, 12, 24, 56, 87, 92]
console.log(res3); // [92, 87, 56, 24, 12, 9, 7, 5, 1]

擴充套件:

a-b的值:1.返回值小於0,在排序後的陣列中 a 應該出現在 b 之前

                2.返回值小於0,a,b在陣列中順序不變

                3.返回值大於0,在排序後的陣列中 a 應該出現在 b 之後


7.reverse

作用:倒序陣列
引數:無
返回值:倒序後的原陣列
是否改變原陣列:改變
var ary = [1,2,3,4,5];
var res = ary.reverse();
console.log(ary);  // [5, 4, 3, 2, 1]
console.log(res);  // [5, 4, 3, 2, 1]

不改變原陣列方法:

1.concat

作用:用於連線兩個或多個數組
引數:引數可以是具體的值,也可以是陣列物件。可以是任意多個
返回值:返回連線後的新陣列
是否改變原陣列:不改變
var ary = [1,2,3,4,5];
var res = ary.concat(6,7);
var res2 = ary.concat(6,[7,8]);
var res3 = ary.concat(6,[7,[8,9]]);
var res4 = ary.concat();
console.log(ary);  // [1, 2, 3, 4, 5]
console.log(res);  // [1, 2, 3, 4, 5, 6, 7]
console.log(res2);  //[1, 2, 3, 4, 5, 6, 7, 8]
console.log(res3)  // [1, 2, 3, 4, 5, 6, 7, [8,9]]  concat() 如果操作的引數是陣列,那麼新增的是陣列中的元素,而不是陣列。 如果是二維(或以上)陣列,concat只能'拆開'一層陣列
console.log(res4) // [1, 2, 3, 4, 5]  如果concat()沒有引數或者引數是空陣列也可以達到克隆陣列的目的

2.join

作用:用指定的分隔符將陣列每一項拼接為字串
引數:指定的分隔符,如果省略該引數,則使用逗號作為分隔符
返回值:拼接好的字串
是否改變原陣列:不改變
var ary = [1,2,3,4,5];
var res = ary.join('-');
console.log(ary);  // [1, 2, 3, 4, 5]
console.log(res);  // 1-2-3-4-5

3.slice

作用:擷取陣列(複製陣列)
引數:array.slice(start, end)
返回值:返回一個新陣列
是否改變原陣列:不改變
var ary = [1,2,3,4,5];
var res = ary.slice(1,3);
var res2 = ary.slice(-3,-1)
console.log(ary);  // [1,2,3,4,5]
console.log(res);  // [2,3]
console.log(res2)  //[3,4] slice支援負引數,從最後一項開始算起,-1為最後一項,-2為倒數第二項

slice(n) //從索引n開始複製到最後一項
slice()、 slice(0)  //複製整個陣列

4.split

作用:把一個字串分割成字串陣列
引數:array.split(separator, howmany) //separator: 字串或正則表示式, howmany: 返回的陣列最大長度
返回值:返回一個字串陣列
是否改變原陣列:不改變
var str="How are you doing today?"
console.log(str.split(" "))
console.log(str.split(""))
console.log(str.split(" ",3))

結果為:


5.toString

作用:把陣列轉化為字串並返回結果
返回值:返回一個字串
是否改變原陣列:不改變
var arr = ['how','are','you'];
var str = arr.toString();
console.log(str);     //how,are,you
console.log(typeof str);   //string
如果覺得好的話請點贊哦,或者掃描下邊二維碼打賞哦,謝謝