傳參:start表明從哪裏開始截取,end表明截取結束的地方數組
var a = [1,2,3]
a.slice(1);//[2,3]
能夠刪除最後一個元素並返回刪除的最後一個元素code
var a = [1,2,3]
a.pop();//3
能夠刪除第一個元素並返回刪除的第一個元素字符串
var a = [1,2,3]
a.shift();//1
傳參:傳參的規則方法 升序的方法 重要!!!!string
function sortNumber(a,b)
{
return a - b
}
var arr = new Array(6)
arr[0] = "10"
arr[1] = "5"
arr[2] = "40"
arr[3] = "25"
arr[4] = "1000"
arr[5] = "1"
document.write(arr.sort(sortNumber))//1,5,10,25,40,1000
傳參:start表明開始刪除/添加的位置 必選
number:刪除的個數 必選
item1, ..., itemX:添加的元素 可選
it
var a = [1,2,3,4,5,6]
a.splice(2,0,9);//[]
console.log(a);//[1, 2, 9, 3, 4, 5, 6]
將一個數組拼接在另外一個數組後面
返回值:拼接後的數組
原來的數組不變io
var a = [1,2,3,4]
var b = [5,6,7]
console.log(a.concat(b));//[1,2,3,4,5,6,7]
console.log(a);//[1,2,3,4]
把數組變成用「,」鏈接的字符串console
var a = [1,2,3,4]
a.join();//"1,2,3,4"
將字符串以規定的規則進行分割成數組
傳參:Rex意思是分割的規則 必選
length是分割的長度,非必須function
var a = "hello world lar"
a.split(" ");//["hello","world","lar"]
a.split(" ",2);//["hello","world"]
判斷是否包含子字符串方法
var a = "hello world lar";
var b = ["hello","world","lar"];
a.indexOf("hello");//0
b.indexOf("hello");//0
b.indexOf(123);//-1
var a = [1,2,3,4,5]
var b = [52,3,3,4,6]
var c = [];
for(var i = 0;i < a.length;i++){
for(var j = 0;j < b.length;j++){
if(a[i] == b[j]){
c.push(a[i]);
break
}
}
}
console.log(c); //[3,4]
var a = [1,2,34,5,6];
var b = [7,8,9];
for(var i = 0;i
a.splice(2+i,0,b[i]);
}
console.log(a);//[1, 2, 7, 8, 9, 34, 5, 6]
var a = [1,2,34,5,6];//刪掉 34,5 a.splice(1,2); console.log(a);//[1, 5, 6]