0

I just recently discovered the "Set" data type in ES6. Love it so far. I am working on a Autoselect which consists of multiple options, but I don't want to show duplicates. Thats why I use the Set-Datatype.

For example:

I have this array of uploads:

const uploads = [
 {projectNr: PUE-200, projectDesc: "This is project 200"},
 {projectNr: PUE-200, projectDesc: "This is project 200"},
 {projectNr: PUE-201, projectDesc: "This is project 201"},
 {projectNr: PUE-201, projectDesc: "This is project 201"},
 {projectNr: PUE-202, projectDesc: "This is project 202"}
]

I need an array for my Autoselector consisting of an instance of every project but no duplicates. I managed to retrieve the project numbers like this (PUE-200, PUE-201, PUE-202 and so on) but I also need the description. My approach was to split the project into to Sets. One for the project number and the other one for the description. Both Sets are complete but now I need them in an array. I tried to use the spread operator, it kinda works but I need both values as an actual value of an array element in order to map it.

My current situation:

 ["PUE-200", "This is project 200", "PUE-201", "This is project 201"]

What I need:

 [
  {ProjektNr:"PUE-200", ProjektDesc: "This is project 200",
  {ProjektNr:"PUE-201", ProjektDesc: "This is project 201""}
 ]

Edit: This is my current code:

const uniqueProjektNr = new Set();
const uniqueProjektBeschreibung = new Set();

uploads.map((upload) => {
    uniqueProjektNr.add(upload.ProjektNr);
    uniqueProjektBeschreibung.add(upload.ProjektBeschreibung);
  });

let uniqueProjektHolder = [...uniqueProjektNr, ...uniqueProjektBeschreibung];
  • I have an idea what can be done, but without a tangible [Minimal, Complete, and Reproducible Code Example](https://stackoverflow.com/help/minimal-reproducible-example) of your attempt it could be irrelevant. Please do update your question to include some code that you've tried. – Drew Reese Jun 08 '21 at 07:27
  • The last snippet in this answer: https://stackoverflow.com/a/18773857/3082296 – adiga Jun 08 '21 at 07:28
  • @adiga I think this is different, I think OP wants to essentially merge two arrays. – Drew Reese Jun 08 '21 at 07:31
  • @DrewReese Updated my question with the code, appricate any help – Lee Everett Jun 08 '21 at 07:32
  • @adiga No I need to merge two Set-Collections into a single Array – Lee Everett Jun 08 '21 at 07:33
  • Are there any objects which have same `projectNr` and different `projectDesc`? Are they considered different objects? Your input doesn't show that. Also, your output doesn't have `"PUE-202"` – adiga Jun 08 '21 at 07:36
  • @adiga Really thankful for your help, but the answer below solved my problem. Thank you nevertheless :) – Lee Everett Jun 08 '21 at 07:54

1 Answers1

0

const uploads = [
 {projectNr: 'PUE-200', projectDesc: "This is project 200"},
 {projectNr: 'PUE-200', projectDesc: "This is project 200"},
 {projectNr: 'PUE-201', projectDesc: "This is project 201"},
 {projectNr: 'PUE-201', projectDesc: "This is project 201"},
 {projectNr: 'PUE-202', projectDesc: "This is project 202"}
]

const output = uploads.reduce((distinctOptions, option) => {
  const isDuplicate = distinctOptions.some(newOption => option.projectNr === newOption.projectNr && option.projectDesc === newOption.projectDesc)
  if(!isDuplicate){
      distinctOptions.push(option)
  }
  return distinctOptions
} ,[])

console.log(output)
Shyam
  • 5,292
  • 1
  • 10
  • 20