给定此对象数组:
[ { source_q_id: 'Q2B', target_q_id: null },
{ source_q_id: 'Q2B', target_q_id: null },
{ source_q_id: 'Q2B', target_q_id: null },
{ source_q_id: 'Q5C', target_q_id: 'Q7' },
{ source_q_id: 'Q5C', target_q_id: 'Q7' },
{ source_q_id: 'Q5C', target_q_id: 'Q7' },
{ source_q_id: 'Q5D', target_q_id: 'Q7' },
{ source_q_id: 'Q5D', target_q_id: 'Q7' },
{ source_q_id: 'Q5D', target_q_id: 'Q7' },
{ source_q_id: 'Q6A1', target_q_id: 'Q7' },
{ source_q_id: 'Q6A1', target_q_id: 'Q7' },
{ source_q_id: 'Q6A1', target_q_id: 'Q7' },
{ source_q_id: 'Q6A2', target_q_id: null },
{ source_q_id: 'Q6A2', target_q_id: null },
{ source_q_id: 'Q6A3', target_q_id: 'Q7' },
{ source_q_id: 'Q6A3', target_q_id: 'Q7' },
{ source_q_id: 'Q6A3', target_q_id: 'Q7' },
{ source_q_id: 'Q6B', target_q_id: 'Q6A2' },
{ source_q_id: 'Q6B', target_q_id: 'Q6A2' },
{ source_q_id: 'Q7', target_q_id: null },
{ source_q_id: 'Q7', target_q_id: null }]
对于键/值,我需要一个新的独特dupe数组:
[ { source_q_id: 'Q2B', target_q_id: null },
{ source_q_id: 'Q5C', target_q_id: 'Q7' },
{ source_q_id: 'Q5D', target_q_id: 'Q7' },
{ source_q_id: 'Q6A1', target_q_id: 'Q7' },
{ source_q_id: 'Q6A2', target_q_id: null },
{ source_q_id: 'Q6A3', target_q_id: 'Q7' },
{ source_q_id: 'Q6B', target_q_id: 'Q6A2' },
{ source_q_id: 'Q7', target_q_id: null }]
我使用了此SO answer中的代码,但它删除了太多target_q_id
个独特的欺骗,因为它不计算source_q_id
欺骗:<\ n / p>
[ { source_q_id: 'Q2B', target_q_id: null },
{ source_q_id: 'Q5C', target_q_id: 'Q7' },
{ source_q_id: 'Q6B', target_q_id: 'Q6A2' } ]
代码:
function dupesOnly(arr, 'target_q_id') {
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;
}
如何修改代码以查找source_q_id
和target_q_id
的唯一欺骗?
答案 0 :(得分:2)
您可以简单地组合这些属性,并将其用作您的集合中的键
另一个注意事项:如果您.push
中有.forEach
项,那么您最有可能做错了。
尝试使用Array.prototype.filter
:
Array.prototype.uniqueBy = function(keyBuilder) {
var seen = {};
return this.filter(function(o) {
// build a filter key using a provided function
var key = keyBuilder(o);
// if item already exists - do not add to the result
if (seen[key])
return false;
// add item to the set and add item to the result
// shortand for:
// seen[key] = true; return true;
return (seen[key] = true);
});
}
var obj = [{source_q_id:'Q2B',target_q_id:null},{source_q_id:'Q2B',target_q_id:null},{source_q_id:'Q2B',target_q_id:null},{source_q_id:'Q5C',target_q_id:'Q7'},{source_q_id:'Q5C',target_q_id:'Q7'},{source_q_id:'Q5C',target_q_id:'Q7'},{source_q_id:'Q5D',target_q_id:'Q7'},{source_q_id:'Q5D',target_q_id:'Q7'},{source_q_id:'Q5D',target_q_id:'Q7'},{source_q_id:'Q6A1',target_q_id:'Q7'},{source_q_id:'Q6A1',target_q_id:'Q7'},{source_q_id:'Q6A1',target_q_id:'Q7'},{source_q_id:'Q6A2',target_q_id:null},{source_q_id:'Q6A2',target_q_id:null},{source_q_id:'Q6A3',target_q_id:'Q7'},{source_q_id:'Q6A3',target_q_id:'Q7'},{source_q_id:'Q6A3',target_q_id:'Q7'},{source_q_id:'Q6B',target_q_id:'Q6A2'},{source_q_id:'Q6B',target_q_id:'Q6A2'},{source_q_id:'Q7',target_q_id:null},{source_q_id:'Q7',target_q_id:null}];
var objFiltered = obj.uniqueBy(function(o) {
return o.source_q_id + "~~~" + o.target_q_id;
});
console.log(objFiltered);
&#13;