0

So I have an array of objects and I am trying to remove the duplicates. For example, lets say my array of object contains this data:

[{TERM_CD: "220181", CRS_SUBJ_CD: "LALS", CRS_SUBJ_DESC: "Latin American &Latino Studies", CRS_NBR: "127", CRS_TITLE: "Latin American Music", …}, 
{TERM_CD: "220101", CRS_SUBJ_CD: "MUS", CRS_SUBJ_DESC: "Music", CRS_NBR: "127", CRS_TITLE: "Latin American Music", …}, etc...]

And this is the removeDup function that I am using to remove duplicates:

removeDup(data, key) {
    return [
      ...new Map(
        data.map(x=>[key(x), x])
      ).values(),
    ];
  }

For example if I call the remove dup function like this then one of the object will be removed since they both have same crs_title.

const noDupArr = this.removeDup(printArr, x => x.CRS_TITLE);

So my goal is to try to remove duplicate on 2 keys CRS_SUBJ_CD and CRS_NBR but I am not able to figure out how to proceed doing that. I tried adding another key parameter in the removeDup function but wasn't successful. Any ideas on fixing this issue. Thank you in advance!

Shalin Patel
  • 169
  • 2
  • 12

1 Answers1

1

You can maintain an array of keys for filtering and then make use of Map to get unique values. Here is an example with duplicate data:

var data=[{TERM_CD: "220181", CRS_SUBJ_CD: "LALS", CRS_SUBJ_DESC: "Latin American &Latino Studies", CRS_NBR: "127", CRS_TITLE: "Latin American Music"},
{TERM_CD: "220101", CRS_SUBJ_CD: "MUS", CRS_SUBJ_DESC: "Music", CRS_NBR: "127", CRS_TITLE: "Latin American Music"},{TERM_CD: "220101", CRS_SUBJ_CD: "MUS", CRS_SUBJ_DESC: "Music", CRS_NBR: "127", CRS_TITLE: "Latin American Music"}];

var keys = ['CRS_SUBJ_CD',  'CRS_NBR'];

var result = [...new Map(data.map(p=>[keys.map(k=>p[k]).join('|'),p])).values()];

console.log(result);

I hope this helps. Thanks!

gorak
  • 5,233
  • 1
  • 7
  • 19
  • False positives: `{CRS_SUBJ_CD:"foo", CRS_NBR:"bar"}` and `{CRS_SUBJ_CD:"foo|bar", CRS_NBR:""}` OR `{CRS_SUBJ_CD:"foo ", CRS_NBR:"|bar"}` and `{CRS_SUBJ_CD:"foo |", CRS_NBR:"bar"}` – smac89 Jun 26 '20 at 18:42