Comment detail
アレイのuniq (Nested Flatten)This comment is reply for 450 id:amachang: うーん。プリミティブ値だけの配列だったら...(アレイのuniq). Go to thread root.
いっそ両方とも実装してみるとか。ダメ?
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 37 38 39 40 41 42 | Array.prototype.uniq = function(usedObjKey) {
if (typeof(usedObjKey) == 'undefined')
usedObjKey = (typeof(this[0]) == 'object');
var ret = [];
if (usedObjKey) {
for (var i=0, len=this.length, s=[]; i<len; i++) {
if (s.indexOf(this[i]) < 0) {
ret.push(this[i]);
s.push(this[i]);
}
}
}
else
{
for (var i=0, len=this.length, s={}; i<len; i++) {
if (!s[this[i]])
ret.push(this[i]);
s[this[i]] = true;
}
}
return ret;
}
/**
* @site http://developer.mozilla.org/ja/docs/
* Core_JavaScript_1.5_Reference:Global_Objects:Array:indexOf
*/
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(elt /*, from*/) {
var len = this.length;
var from = Number(arguments[1]) || 0;
from = (from < 0) ? Math.ceil(from) : Math.floor(from);
if (from < 0)
from += len;
for (; from<len; from++)
if (from in this && this[from] === elt)
return from;
return -1;
};
}
|





id:amachang
#463()
[
JavaScript
]
Rating0/0=0.00
Object Type にも対応した書き方。 効率悪いとか言わない>< alert([1, 2, {}, {}, 2, 1].uniq()); // [1, 2, {}, {}] var a = {}; alert([1, 2, a, a].uniq()); // [1, 2, {}]Rating0/0=0.00-0+