@bluexiaowei
2016-09-04T13:58:07.000000Z
字数 2955
阅读 562
javascript
@来源互联网
@整理: bluexiaowei
Array.prototype.unique1 = function(arr) {
var
i,
length = this.length,
mapArray = [],
hash = {};
for (i = 0; i < length; i += 1) {
!hash[this[i]] && (
hash[this[i]] = true,
mapArray.push(this[i])
);
}
if(arr){
for(i = 0, length = arr.length; i < length; i += 1) {
!hash[arr[i]] && (
hash[arr[i]] = true,
mapArray.push(arr[i])
);
}
}
return mapArray;
};
Array.prototype.unique2 = function() {
var
i,
length = this.length,
mapArray = [];
this.sort();
for (i = 0; i < length; i += 1){
this[i] != this[i + 1] && mapArray.push(this[i]);
}
return mapArray;
}
Array.prototype.unique3 = function() {
var
i,
length = this.length;
mapArray = [];
for (i = 0; i < length; i += 1){
mapArray.indexOf(this[i]) == -1 && mapArray.push(this[i]);
}
return mapArray;
};
Array.prototype.unique4 = function() {
var
i,
length = this.length;
mapArray = [this[0]];
for (i = 1; i < length; i +=1) {
this.indexOf(this[i]) == i && mapArray.push(this[i]);
}
return mapArray;
}
true
、去异留同 false
function reserve(arr1, arr2, mode) {
var
i,
length,
key,
mapArray = [],
hash = {};
for(i = 0, length = arr2.length; i < length; i += 1) {
!hash[arr2[i]] && (
hash[arr2[i]] = true,
mapArray.push(arr2[i])
);
}
arr2 = mapArray;
mapArray = [];
hash = {};
for(i = 0, length = arr1.length; i < length; i += 1) {
hash[arr1[i]] = true;
}
for(i = 0, length = arr2.length; i < length; i += 1) {
if(mode) {
hash[arr2[i]] && mapArray.push(arr2[i]);
} else {
hash[arr2[i]] ? delete hash[arr2[i]] : mapArray.push(arr2[i]);
for(key in hash) {
if(hash.hasOwnProperty(key)) {
mapArray.push(key);
}
}
}
}
return mapArray;
}
/*升序*/
Array.sort();
/*降序*/
array = array.sort( function( a, b ) { return b - a } );
/*乱序*/
array = array.sort( function() { return Math.random() - 0.5 } );
/* 选择排序 */
Array.prototype.selectionSort = function() {
var
i,
j,
cache,
index,
length = this.length;
for(i = 0; i < length; i += 1) {
cache = this[i];
index = i;
for(j = i + 1; j < length; j += 1) {
cache > this[j] && (
cache = this[j],
index = j
)
}
this[index] = this[i];
this[i] = cache;
}
return this;
};
/* 冒泡排序 */
Array.prototype.bubbleSort = function() {
var
i,
j,
yes,
cache,
length = this.length;
for(i = 0; ;i += 1) {
yes = true;
for(j = 0; j < length; j += 1) {
this[j] > this[j + 1] && (
cache = this[j + 1],
this[j + 1] = this[j],
this[j] = cache,
yes = false
);
}
if(yes) return this;
}
};
/* 随机排序 */
Array.prototype.shuffle = function() {
var
j,
x,
i = this.length;
for(; i;) {
j = parseInt(Math.random() * i);
x = this[--i];
this[i] = this[j];
this[j] = x;
};
return this;
};
/*内置方法*/
var arr = arr1.concat(arr2);
/*优化*/
var arr = Array.prototype.push.call(arr1,arr2);
/*或者*/
arr1.push.call(arr1,arr2);
/*减小数组长度*/
var
arr = [1, 2, 3, 4, 5, 6, 7];
console.log(arr);[1, 2, 3, 4, 5, 6, 7];
arr.length = 3;
console.log(arr);//[1, 2, 3]
var arr, array = [ 1, 2, 3, 4, 5, 6 ];
arr = array.slice();
console.log( arr ); //[ 1, 2, 3, 4, 5, 6 ]
arr = array.slice( 2, 5 );
console.log( arr ); //[ 3, 4, 5 ]
arr = array.slice( -2, 5 );
console.log( arr ); //[ 5 ]
arr = array.slice( -2, -1 );
console.log( arr ); //[ ]
arr = array.slice( -3 );
console.log( arr ); //[ 4, 5, 6 ]
javscript 中数组类型分两种,原始类型、对象类型。都可以用浅克隆。除了对象类型中的对象得用深度克隆。
function clone(object){
var
key,
newObj,
obj = Object.prototype.toString.call(object).slice(8,-1);
if(obj !== 'Object'){
return newObj = object;
} else if (window.JSON) {
newObj = JSON.stringify(object);
newObj = JSON.parse(newObj);
return newObj;
} else {
for(key in object){
newObj[key] = object[key];
}
return newObj;
}
}