-3
const data = [{
    id: 1,
    Group: 'A',
    Name: 'SD'
}, {
    id: 2,
    Group: 'B',
    Name: 'FI'
}, {
    id: 3,
    Group: 'A',
    Name: 'SD'
}, {
    id: 4,
    Group: 'B',
    Name: 'CO'
}];

let unique = [...new Set(data.map(item => item.Group))];
console.log(unique);

Expecting only return district array object in the data 0 index value should not return again as it does already have same group and name value.

https://jsfiddle.net/k3p7xfcw/

Thank You

simon
  • 359
  • 1
  • 14
  • https://stackoverflow.com/questions/2218999/how-to-remove-all-duplicates-from-an-array-of-objects – cmgchess Mar 28 '23 at 17:31
  • 1
    `data.filter((v,i,a)=>a.findIndex(v2=>(v2.Group===v.Group && v2.Name===v.Name))===i)` – cmgchess Mar 28 '23 at 17:36
  • You could convert the array to nested objects and use the properties as keys. Then, convert it back to an array. That's much faster than `filter` with `findIndex` for large datasets. – Thomas Sablik Mar 28 '23 at 17:44

2 Answers2

0

You can try Unique by multiple properties ( Group and Name )

let result = data.filter((v,i,a)=>a.findIndex(v2=>['Group','Name'].every(k=>v2[k]
===v[k]))===i)

https://jsfiddle.net/G2jakhmola/qutranps/1/

G2 Jakhmola
  • 796
  • 1
  • 5
  • 15
-1

using JS array filter

const data = [{
    Group: 'A',
    Name: 'SD'
}, {
    Group: 'B',
    Name: 'FI'
}, {
    Group: 'A',
    Name: 'SD'
}, {
    Group: 'B',
    Name: 'CO'
}];

const uniqueGroups = data.filter((item, index, array) => {
  // Return true only if the current item is the first occurrence of the group in the array
  return array.findIndex((t) => t.Group === item.Group) === index;
});

console.log(uniqueGroups); // [{Group: 'A', Name: 'SD'}, {Group: 'B', Name: 'FI'}]
Azad
  • 5,144
  • 4
  • 28
  • 56