// filterProperty i.e. 'id' or 'name'
function GetDuplicates(array, filterProperty) {
//lookup with our intended filter to get the duplicates by a given property
const lookup = array.reduce((a, e) => {
a[e.filterProperty] = ++a[e.filterProperty] || 0;
return a;
}, {});
let duplicates = array.filter(e => lookup[e.filterProperty]);
return duplicates;
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter