JavaScript习题(数组去重、有序数组乱序排列)

版权声明:转载请标明出处 https://blog.csdn.net/Ms_yjk/article/details/86649814

数组去重

Array.prototype.unique = function(){
			var temp = {},
				arr = [],
				len = this.length;
			for(var i = 0 ; i < len ; i ++){
				if(!temp[this[i]]){
					temp[this[i]] = "abc";		//this[i]   0有bug
					arr.push(this[i]);
				}
			}
			return arr;
		}

将有序数组进行乱序排列

使用Math.random()函数

var arr = [1,2,3,4,5,6,7];
		arr.sort(function(){
			return Math.random() - 0.5; 
		});

猜你喜欢

转载自blog.csdn.net/Ms_yjk/article/details/86649814
今日推荐