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

如果覺得好的話請點贊哦,或者掃描下邊二維碼打賞哦,謝謝

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *