例:javascript
var arr = [1,2,3]; arr.join(); ==> 輸出'1,2,3' arr.join(" ") ==> 輸出'1 2 3' arr.join("") ==> 輸出'123' //此時再次打印arr console.log(arr); ==> 輸出[1,2,3]
例:java
var arr = [1,4,3]; arr.reverse(); ==> 輸出[3,4,1] //此時再次打印arr console.log(arr); ==> 輸出[3,4,1]
例:數組
var arr = ['abc','cfg','1',undefined] arr.sort(); ==> 輸出["1", "abc", "cfg", undefined] var arr1 = [2,5,3,7,6]; //返回值小於0,從小到大進行排列 arr1.sort(function (a,b) {return a - b}); ==> 輸出[2, 3, 5, 6, 7] //返回值大於0,從大到小進行排列 arr1.sort(function (a,b) {return b - a}); ==>輸出[7, 6, 5, 3, 2] //此時輸出arr1 console.log(arr1); ==>輸出[7, 6, 5, 3, 2]
例:函數
var arr = [1,3]; arr.concat(5,4,[6,7,[8,9]],{s:'1'}); ==> 輸出[1,3,5,4,6,7,[8,9],{s:'1'}] //此時輸出arr console.log(arr); ==>輸出[1,3]
例:code
var arr = [1,2,3,4]; arr.slice(0,2); ==> 輸出[1,2] arr.slice(1); ==> 輸出[2,3,4] arr.slice(-1,-3); ==> 輸出[] arr.slice(-3,-2); ==> 輸出[2] arr.slice(1,-1); ==> 輸出[2,3] //此時輸出arr console.log(arr) ==> 輸出[1,2,3,4]
例:排序
var arr = [1,2,3,4]; var a = arr.splice(2); ==> 輸出a的值爲[3,4],此時arr的值爲[1,2] //上下對應的是不一樣的方式運行的結果 var a = arr.splice(1,2); ==>輸出a的值爲[2,3],此時arr的值爲[1,4] var a = arr.splice(1,2,5,6); ==>輸出a的值爲[2,3],此時arr的值爲[1,5,6,4] var a = arr.splice(1,2,[5,6]); ==>輸出a的值爲[2,3],此時arr的值爲[1,[5,6],4] //該方法區別於concat,它會把指定插入的什麼,就會插入什麼,不會作出改變
例:ip
var arr = [1,2]; var a = arr.push([],{},3,[1,2],{s:'haha'}); ==>輸出a的值爲6 //原數組arr爲[1,2,[],{},[1,2],{s:'haha'}]
例:字符串
var arr = [1,2,3]; var a = arr.pop(); ==>輸出a的值爲3,原數組arr的值爲[1,2] //pop裏面傳參數是沒有用的,返回的都是數組最後一位
var arr = [1,2,3]; var a = arr.unshift(4,5,6); ==>輸出a的值6,原數組arr爲[4,5,6,1,2,3]
var arr = [1,2,3]; var a = arr.shift(); ==>輸出a的值爲1,原數組arr爲[2,3] //shift方法裏面傳參沒有什麼用,返回的都是數組的第一位
注意:輸出不包括方括號或其它任何形式的包裹數組值得分隔符get
var arr = ['a',1,[2,3],[],{},{s:'hello'}]; var a = arr.toString(); //輸出a的值爲"a,1,2,3,,[object Object],[object Object]"