數(shù)組API:
(1).String();把數(shù)組轉(zhuǎn)換成字符串
var arr=[1,2,3,4,5];
var str=String(arr);
console.log(str);
(2)join('拼接符');拼接,最后拼接成的是字符串
如果沒(méi)有拼接符,默認(rèn)等于String;
var arr=['a','b','c','d','e','f'];
var str=arr.join();
var str=arr.join('');
var str=arr.join('-');
console.log(str);
(3)? concat();拼接? 結(jié)果還為數(shù)組
var arr=['jin','dai','shun'];
var arr2=[5,2,1];
var srt=arr.concat(6666666);
var srt=arr.concat(arr2);
console.log(srt);
(4)slice(starti,endi+1);截取
特點(diǎn):1 含頭不含尾
2 如果省略第二個(gè)參數(shù),默認(rèn)從starti開(kāi)始截取到結(jié)尾
3. 如果一個(gè)參數(shù)都不寫(xiě)恨诱,相當(dāng)于復(fù)制原數(shù)組4. 支持負(fù)數(shù)參數(shù)
var arr=['a','b','c','d','e','f','g'];
? ? ? ? ? ? ? 0? 1? 2? 3? 4? 5? 6
var srt=arr.slice(1,2+2);
var srt=arr.slice(2);
var srt=arr.slice();
var srt=arr.slice(-4,-1);
console.log(srt);
(5)splice(starti,n);? 刪除? 插入? 替換
var arr=['a','b','c','d','e','f','g'];
刪除:
var str=arr.splice(3,2);
插入:
var str=arr.splice(3,0,'1','3');
替換:
var str=arr.splice(3,2,'A','B');? ? console.log(arr);
(6)reverse();? ? 翻轉(zhuǎn)
var arr=[1,2,3,4,5,6];
var str=arr.reverse();
console.log(str);
(7)push();? 給數(shù)組末尾添加元素
var arr=[1,2,3,4,5];
var str=arr.push('X','O');
console.log(arr);
(8)pop();? 刪除數(shù)組末尾元素
var arr=[1,2,3,4,5];
var str=arr.pop();
console.log(arr);
(9)unshift();? 給數(shù)組前端添加元素
var arr=[1,2,3,4,5];
var str=arr.unshift('A');
console.log(arr);
(10)shift();? 刪除前端元素
var arr=[1,2,3,4,5];
var str=arr.shift();
console.log(arr);