陣列的方法

2021-10-02 05:48:58 字數 1943 閱讀 4351

陣列的常用方法

1,shift()方法:把陣列的第乙個元素刪除,並返回第乙個元素的值

var a = ['a', 'b', 'c'];

console.log(a,a.shift());

//['b','c'] 'a'

2,unshift() :將引數新增到原陣列開頭,並返回陣列的長度

var movepos =[111,222,333,444];

movepos.unshift("55555")

document.write(movepos + "

") //55555,111,222,333,444

3,pop():用於刪除並返回陣列的最後乙個(刪除元素)元素,如果陣列為空則返回undefined ,把陣列長度減 1

var a = ['a', 'b', 'c'];

console.log(a,a.pop());

//["a", "b"] "c"

4,push():可向陣列的末尾新增乙個或多個元素,並返回新的長度,(用來改變陣列長度)。

var movepos=[11,22];

var arr=movepos.push("333");

console.log(movepos,arr) //[11, 22, "333"] 3

5,concat()方法:用於連線兩個或多個陣列,並返回乙個新陣列,新陣列是將引數新增到原陣列中構成的

var movepos=[11,22];

var arr=movepos.concat(4,5);

console.log(arr);//[11, 22, 4, 5]

6,join()方法:用於把陣列中的所有元素放入乙個字串。元素是通過指定的分隔符進行分隔的。

var movepos=[11,22];

var arr=movepos.join("+");

console.log(arr) //11+22

7,slice()方法:可從已有的陣列中返回選定的元素。slice(開始擷取位置,結束擷取位置)

var movepos=[11,22,33];

var arr=movepos.slice(1,2);

console.log(arr)//[22]

8,splice()方法:方法向/從陣列中新增/刪除專案,然後返回被刪除的專案。

var movepos=[11,22,33,44];

var arr=movepos.splice(1,2);//刪除

console.log(arr,movepos)

[22, 33]  [11, 44]

var movepos =[111,222,333,444];

movepos.splice(2,1,"666")

console.log(movepos)

[111, 222, "666", 444]

split:方法用於把乙個字串分割成字串陣列。

var host="?name=232&key=23";

host=host.split("?")

console.log(host)

["", "name=232&key=23"]

substring() 方法用於提取字串中介於兩個指定下標之間的字元。

var host="?name=232&key=23";

host=host.substring(1)

console.log(host)

//name=232&key=23

陣列的方法

var arr 1,2,3,4,5,6 運算元組的方法 末尾新增 返回長度 尾部移除,返回刪除的數 按照字串的形式排序 function a,b 從小到大 b a大到小 頭部刪除,返回刪除的數 頭部新增 返回長度 顛倒陣列中的元素 把陣列的所有元素放入乙個字串.元素通過指定的分隔符進行分割 刪除元素...

陣列的方法

var arr 陣列的宣告 var arr1 1,s false,范文陣列中的資料 更改使用 陣列每乙個資料都有對應的索引號 0 var num1 arr1 0 var str arr1 1 console.log num1 console.log str console.log num1 str ...

陣列的方法

最近一直在尋找關於陣列的操作最優解 發現了好多陣列方法 indexof 查詢某乙個元素,返回下標,一般是查詢陣列元素 lastindexof 從後面開始查詢元素,返回下標。用法同上 includes 查詢某乙個元素,返回true false,用法同indexof,這個可以差nan。push 從後面插...