You can achieve this in vanilla javascript by using the Array.reduce method. First group the data by id, then divide the groups into unique and duplicate arrays. If you a group has only one item, the item is unique, or if the group has more than one entry, the containing entries are duplicates.
const originialData = [
{id: 1, description: 'abc' },
{id: 2, description: 'def' },
{id: 1, description: 'ghi' },
{id: 3, description: 'jkl' },
{id: 2, description: 'mno' },
{id: 4, description: 'pqr' }
]
// Group data as an object by using the Array.reduce method.
const groupedById = originialData.reduce((groups, item) => {
// if a group has not yet been created, default to empty array.
if (!groups[item.id]) {
groups[item.id] = [];
}
// push the entry into the group
groups[item.id].push(item);
return groups;
}, {});
// Get the group keys
const groupKeys = Object.keys(groupedById);
// Iterate the group keys, and divide into unique and duplicates array
const [unique, duplicates] = groupKeys.reduce(([uniq, dupl], id) => {
// Get the current group
const group = groupedById[id];
// Select array depending on length
if (group.length === 1) { // if length is 1, then the item is unique
uniq = uniq.concat(group);
} else { // else the containing items are duplicates.
dupl = dupl.concat(group);
}
return [uniq, dupl];
}, [[], []])
console.log(unique)
Working example here: https://jsbin.com/sijidacile/edit?js,console