1. join()
1 Array.join() 是 String.split() 的逆向操作 2 3 var arr = [1, 2, 3] 4 arr.join()// "1,2,3" 5 arr.join("")// "123" 6 arr.join(" ")// "1 2 3" 7 arr.join('-')// "1-2-3" 8 arr.join('_')// "1_2_3" 9 arr.join('a')// "1a2a3" 10 11 var arr1 = [1, 2, 3, {a:1,b:2,c:3}] 12 arr1.join('__') // "1__2__3__[object Object]" 13 var arr2 = [1, 2, 3, [1,2,3]] 14 arr2.join('__')// "1__2__3__1,2,3"
数组元素全部倒过来
返回一个新的数组(不修改原数组)
1 var arr = [1, 2, 3]; 2 arr.concat(4, 5) // [1,2,3,4,5] 3 arr.concat([4, 5]) // [1,2,3,4,5] 4 arr.concat([1,2,3], [4, 5]) // [1, 2, 3, 1, 2, 3, 4, 5] 5 arr.concat([1,[2],3], [4, 5]) //[1, 2, 3, 1, [2], 3, 4, 5]
返回数组片段 (不修改原数组) start, end 均是开区间
1 var arr = [1, 5, 6, 7, 3]; 2 arr.slice(0) // [1, 5, 6, 7, 3] 3 arr.slice(1, 2) // [5] 4 arr.slice(1, 3) // [5, 6]
插入或者删除 (修改数组本身)
1 var arr = [1, 5, 6, 7, 3]; 2 arr.splice(1) // [5, 6, 7, 3] 3 4 var arr1 = [1, 5, 6, 7, 3]; 5 arr1.splice(2) // [6, 7, 3] 6 7 var arr2 = [1, 5, 6, 7, 3]; 8 arr2.splice(1,2) // [5, 6] 9 10 var arr3 = [1, 5, 6, 7, 3]; 11 arr3.splice(2,0, 'a') 12 13 arr3 //[1, 5, "a", 6, 7, 3]
判断是数组还是对象
1 var o = {}; 2 try { 3 o.map(i => i) && 'o is arr'} 4 catch(err) { 5 'o is: obj'; 6 }
原文地址:https://mp.weixin.qq.com/s/qedx5LWXfNeT5aachyBjkA
转载于:https://www.cnblogs.com/ZachChan/p/6186528.html