javascript - Find objects in an array that are the same, not remove them -
i'm trying find objects in array same flag them in ui. can't seem use undescore it.
i doing this:
var = [ {'id': 1, 'name': 'jake' }, {'id':4, 'name': 'jenny'}, {'id': 9, 'name': 'nick'}, {'id': 1, 'name': 'jake' } ]; var eaches = _.each(a, function (obj) { _.find(a, function () { return _.isequal(a, obj); }); });
thanks in advance!
seems need this:
var = [{ 'id': 1, 'name': 'jake' }, { 'id': 4, 'name': 'jenny' }, { 'id': 9, 'name': 'nick' }, { 'id': 1, 'name': 'jake' }]; var eq = []; _.each(a, function (x, i) { var e = _.find(a, function (y, j) { return !== j && _.isequal(x, y); }); if (e) { eq.push(x); } }); console.log(eq);
http://jsfiddle.net/f0t0n/wbbs5/
update:
custom "_.uniq
" based on _.isequal
instead of ===
strict comparison:
var uniqeq = _.reject(eq, function(x, i) { return _.find(eq, function(y, j) { return < j && _.isequal(x, y); }); });
Comments
Post a Comment