This comment is reply for 450 id:amachang: うーん。プリミティブ値だけの配列だったら...(アレイのuniq). Go to thread root.
sybian #570(2007/07/11 13:00 GMT) [ JavaScript ] Rating0/0=0.00
ObjectやFunctionにも対応してみた。firefox2.0.0.4+Firebugで確認。 なんというかtoJSONString()の手抜き実装みたいだ。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
Array.prototype.uniq=function(){ var h={}; var tostr=function(obj){ if(typeof obj == "object"){ var str=""; for(var p in obj){ str+=p+":"+arguments.callee(obj[p]); } return "{"+str+"}"; } return obj; } for(var i=0,h={},l=this.length,r=[]; chk=tostr(this[i]),i<l; i++){ if(!h[chk]===true)r.push(this[i]); h[chk]=true; } return r; } // ------------------- var a=[1,2,[3,4],6,2,3,{},{},[4,3] ,function(){},function(a){},function(){},function(){return this;} ,{"a":"test1"} ,{"a":"nest","b":{"c":"nest2"}} ,{"a":"nest2","b":{"c":"nest3"}} ,{"a":"nest","b":{"c":"nest2"}} ,{"a":"test1"} ,{"a":"test3"} ,[5,12,4],[4,3],[5] ]; a.uniq(); //[1, 2, [3, 4], 6, 3, Object, [4, 3], function(), function(), function() //, Object a=test1, Object a=nest b=Object, Object a=nest2 b=Object, Object a=test3 //, [5, 12, 4], [5]]
Rating0/0=0.00-0+
[ reply ]
sybian #570() [ JavaScript ] Rating0/0=0.00
Rating0/0=0.00-0+