1.数组合并去重
function arrMergeDeduplication(arr1,arr2){ var concatArray = arr1.concat(arr2); //合并数组 var newArray = []; for(var i = 0; i < concatArray.length; i++){ if(newArray.indexOf(concatArray[i]) == -1){ newArray.push(concatArray[i]) } } return newArray;}复制代码
2.数组排序
1.使用sort()方法排序function arraySort(arr){ var arr = arr; var compare = function(x,y){ if(x>y){ return -1; }else if(xarr[j]){ t = arr[j]; arr[j] = arr[i]; arr[i] = t; } } } return arr;}复制代码
3.数组筛选
var arr = [1,2,3,4,5];var condition = function(x){ if(x>2){ return x; }}var newarr = arr.filter(condition);console.log(newarr) //[3,4,5]复制代码
4.数组与字符串的转换
var arr = [1,2,3,4,5];//转字符串var arrString = arr.toString(); //"1,2,3,4,5"var arrString = arr.join(','); //"1,2,3,4,5"//转数组var arr = arrString.split(','); //[1,2,3,4,5]复制代码
5.数组截取
var arr = ['a', 'b', 'c']; arr.slice(0) // ["a", "b", "c"] arr.slice(1) // ["b", "c"] arr.slice(1, 2) // ["b"] arr.slice(2, 6) // ["c"] arr.slice() // ["a", "b", "c"] 无参数返回原数组 arr.slice(-2) // ["b", "c"] 参数是负数,则表示倒数计算的位置 arr.slice(-2, -1) // ["b"] 复制代码