数组操作
@bluexiaowei
2. 数组去同留异
1. 数组去重
1 Array.prototype.unique1 = function(arr) { 2 var 3 i, 4 length = this.length, 5 mapArray = [], 6 hash = {}; 7 for (i = 0; i < length; i += 1) { 8 !hash[this[i]] && ( 9 hash[this[i]] = true, 10 mapArray.push(this[i]) 11 ); 12 } 13 if(arr){ 14 for(i = 0, length = arr.length; i < length; i += 1) { 15 !hash[arr[i]] && ( 16 hash[arr[i]] = true, 17 mapArray.push(arr[i]) 18 ); 19 } 20 } 21 return mapArray; 22 }; 23 Array.prototype.unique2 = function() { 24 var 25 i, 26 length = this.length, 27 mapArray = []; 28 this.sort(); 29 for (i = 0; i < length; i += 1){ 30 this[i] != this[i + 1] && mapArray.push(this[i]); 31 } 32 return mapArray; 33 } 34 Array.prototype.unique3 = function() { 35 var 36 i, 37 length = this.length; 38 mapArray = []; 39 for (i = 0; i < length; i += 1){ 40 mapArray.indexOf(this[i]) == -1 && mapArray.push(this[i]); 41 } 42 return mapArray; 43 }; 44 Array.prototype.unique4 = function() { 45 var 46 i, 47 length = this.length; 48 mapArray = [this[0]]; 49 for (i = 1; i < length; i +=1) { 50 this.indexOf(this[i]) == i && mapArray.push(this[i]); 51 } 52 return mapArray; 53 }
2. 数组去同留异 true、去异留同 false
1 function reserve(arr1, arr2, mode) { 2 var 3 i, 4 length, 5 key, 6 mapArray = [], 7 hash = {}; 8 for(i = 0, length = arr2.length; i < length; i += 1) { 9 !hash[arr2[i]] && ( 10 hash[arr2[i]] = true, 11 mapArray.push(arr2[i]) 12 ); 13 } 14 arr2 = mapArray; 15 mapArray = []; 16 hash = {}; 17 for(i = 0, length = arr1.length; i < length; i += 1) { 18 hash[arr1[i]] = true; 19 } 20 for(i = 0, length = arr2.length; i < length; i += 1) { 21 if(mode) { 22 hash[arr2[i]] && mapArray.push(arr2[i]); 23 } else { 24 hash[arr2[i]] ? delete hash[arr2[i]] : mapArray.push(arr2[i]); 25 for(key in hash) { 26 if(hash.hasOwnProperty(key)) { 27 mapArray.push(key); 28 } 29 } 30 } 31 } 32 return mapArray; 33 }
3. 数组排序
1 /*升序*/ 2 Array.sort(); 3 /*降序*/ 4 array = array.sort( function( a, b ) { return b - a } ); 5 /*乱序*/ 6 array = array.sort( function() { return Math.random() - 0.5 } ); 7 /* true 升序 false 降序*/ 8 Array.prototype.bubble = function(mode) { 9 var 10 i, 11 j, 12 yes, 13 value, 14 length = this.length; 15 for(i = 0; ;i += 1) { 16 yes = true; 17 for(j = 0; j < length; j += 1) { 18 if (mode){ 19 this[j] > this[j + 1] && ( 20 value = this[j + 1], 21 this[j + 1] = this[j], 22 this[j] = value, 23 yes = false 24 ); 25 } else { 26 this[j] < this[j + 1] && ( 27 value = this[j + 1], 28 this[j + 1] = this[j], 29 this[j] = value, 30 yes = false 31 ); 32 } 33 } 34 if(yes) return this; 35 } 36 }; 37 /*乱序*/ 38 Array.prototype.shuffle = function() { 39 var 40 j, 41 x, 42 i = this.length; 43 for(; i;) { 44 j = parseInt(Math.random() * i); 45 x = this[--i]; 46 this[i] = this[j]; 47 this[j] = x; 48 }; 49 return this; 50 };
4. 数组合并
1 /*内置方法*/ 2 var arr = arr1.concat(arr2); 3 /*优化*/ 4 var arr = Array.prototype.push.call(arr1,arr2); 5 /*或者*/ 6 arr1.push.call(arr1,arr2);
5. 数组截断
1 /*减小数组长度*/ 2 var 3 arr = [1, 2, 3, 4, 5, 6, 7]; 4 console.log(arr);[1, 2, 3, 4, 5, 6, 7]; 5 arr.length = 3; 6 console.log(arr);//[1, 2, 3]
6. 数组截取
1 var arr, array = [ 1, 2, 3, 4, 5, 6 ]; 2 arr = array.slice(); 3 console.log( arr ); //[ 1, 2, 3, 4, 5, 6 ] 4 arr = array.slice( 2, 5 ); 5 console.log( arr ); //[ 3, 4, 5 ] 6 arr = array.slice( -2, 5 ); 7 console.log( arr ); //[ 5 ] 8 arr = array.slice( -2, -1 ); 9 console.log( arr ); //[ ] 10 arr = array.slice( -3 ); 11 console.log( arr ); //[ 4, 5, 6 ]
7. 深度克隆
1 javscript 中数组类型分两种,原始类型、对象类型。都可以用浅克隆。除了对象类型中的对象得用深度克隆。 2 function clone(object){ 3 var 4 key, 5 newObj, 6 obj = Object.prototype.toString.call(object).slice(8,-1); 7 if(obj !== 'Object'){ 8 return newObj = object; 9 } else if (window.JSON) { 10 newObj = JSON.stringify(object); 11 newObj = JSON.parse(newObj); 12 return newObj; 13 } else { 14 for(key in object){ 15 newObj[key] = object[key]; 16 } 17 return newObj; 18 } 19 }

浙公网安备 33010602011771号