0 points

get list of only duplicates from a list of objects with duplicate objects


dashboard

Mon Nov 02 2020 19:31:04 GMT+0000 (UTC)

Posted by @ali_alaraby #javascript

// 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;
}
content_copy Copy