Lodash: What's the opposite of `_uniq()`? -
the _.uniq()
in lodash removes duplicates array:
var tst = [ { "topicid":1,"subtopicid":1,"topicname":"a","subtopicname1":"w" }, { "topicid":2,"subtopicid":2,"topicname":"b","subtopicname2":"x" }, { "topicid":3,"subtopicid":3,"topicname":"c","subtopicname3":"y" }, { "topicid":1,"subtopicid":4,"topicname":"c","subtopicname4":"z" }] var t = _.uniq(tst, 'topicname')
this returns:
[ {"topicid":1,"subtopicid":1,"topicname":"a","subtopicname1":"w" }, { topicid: 2, subtopicid: 2, topicname: 'b', subtopicname2: 'x' }, { topicid: 3, subtopicid: 3, topicname: 'c', subtopicname3: 'y' } ]
what's opposite of this? should return single object each duplicate object:
[ { topicid: 3, subtopicid: 3, topicname: 'c', subtopicname3: 'y' } ]
i don't think there's built in method, here's should job:
function dupesonly(arr, field) { var seen = {}, ret = []; arr.foreach(function(item) { var key = item[field], val = seen[key]; if (!val) { seen[key] = val = { initial: item, count: 0 } } if (val.count === 1) { ret.push(val.initial); } ++val.count; }); return ret; }
Comments
Post a Comment