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];
7
  • I have an idea what can be done, but without a tangible Minimal, Complete, and Reproducible Code Example of your attempt it could be irrelevant. Please do update your question to include some code that you've tried. Commented Jun 8, 2021 at 7:27
  • The last snippet in this answer: stackoverflow.com/a/18773857/3082296 Commented Jun 8, 2021 at 7:28
  • @adiga I think this is different, I think OP wants to essentially merge two arrays. Commented Jun 8, 2021 at 7:31
  • @DrewReese Updated my question with the code, appricate any help Commented Jun 8, 2021 at 7:32
  • @adiga No I need to merge two Set-Collections into a single Array Commented Jun 8, 2021 at 7:33

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.