javascript排序、功能代码总结[长期更新]

 1 //基本数组去重
 2 ///输入:向数组中添加功能函数unique
 3 ///输出:返回一个没有重复元素的数组
 4 Array.prototype.unique = function(){
 5     result =[];
 6     this.forEach(function(x){
 7         if(result.indexOf(x) < 0)
 8             result.push(x);
 9     });
10     return result;
11 };
12 
13 //数字和字符串数组的排序
14 ///输入:向数组中添加功能函数ssort
15 ///输出:返回一个从小到达的排序结果,数字在前,字符串在后
16 ///注意事项:只针对含有数字和字符串的数组
17 var shuzu =[11,'s',234,'a',2,'d',5];
18 
19 Array.prototype.ssort = function(){
20     resultA = [];
21     resultB = [];
22     resultA = this.filter(function(x){
23         if(typeof x === 'string')
24             return true;
25     });
26     resultB = this.filter(function(x){
27         if(typeof x === 'number')
28             return true;
29     });
30     resultA.sort();
31     resultB.sort(function(a,b){
32         return a-b;
33     });
34     this.length = 0;
35     return this.concat(resultB,resultA);
36 };
排序
1 //将字符串返回各个字符含有空格的字符串
2 //方法1
3 String.prototype.addSpace = function(){
4     return Array.prototype.join.call(this,' ');
5 };
6 //方法2
7 String.prototype.addSpace = function(){
8     return this.split('').join(' ');
9 };
字符串操作
//要使sum(2,3)和sum(2)(3)的值是一致的
function sum(a)
{
	if(arguments.length === 1)
		return function(b){
			return a+b;
		};
	else if(arguments.length === 2)
		return arguments[1]+a;
}

  

原文地址:https://www.cnblogs.com/zhangdewang/p/6858207.html