1. 程式人生 > >數組和對象常用方法匯總

數組和對象常用方法匯總

undefine 刪除 epo ole pos host AR 返回 末尾

數組的常用方法

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 + "<br />")     //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

剩下的有時間在不上

參考:https://www.cnblogs.com/js0618/p/6283724.html

數組和對象常用方法匯總