0

How can I create two of same object for same array each pass through .map?

Have a data set like the following:

data = [
  {initiatorRole: "dispatch", receiverRole: "police"},
  {initiatorRole: "dispatch", receiverRole: "fire"},
  {initiatorRole: "police", receiverRole: "dispatch"}
]

I would like to get these into a single set of objects where I will remove duplicates and add additional data.

roles = [
  {role: "dispatch"},
  {role: "police"},
  {role: "dispatch",
  {role: "fire"},
  {role: "police"},
  {role: "dispatch"}
]

The code I currently have:

roles = data.map(d => {
  let rObj = {}
  rObj["id"] = d.initiatorRole
  rObj["id"] = d.receiverRole
  return rObj;
});

But of course this just returns the last rObj set. Can this be done with a single map call?

Toph
  • 2,561
  • 2
  • 24
  • 28
kalmdown
  • 601
  • 1
  • 9
  • 13
  • So...maybe, because as a beginning coder, I didn't think of it that way. So then your response could have been an answer below to the effect..."I wouldn't use .map, I'd use .reduce in this way instead... like the guy who did answer it. – kalmdown Sep 02 '21 at 17:41
  • 1
    Looks like there are a lot of answers to a similar question here: https://stackoverflow.com/questions/46986710/return-multiple-values-from-es6-map-function Btw I tweaked your title, mostly to remove "Observable" as it might confuse future searchers (I think it's a good question for any kind of JavaScript), and fixed a couple quotation marks in your code example. Glad my answer helped, and cheers! – Toph Sep 02 '21 at 18:12
  • @Toph Though I wasn't initially thinking about dups, it is of interest, I wanted to know how return multiple objects. That was what the question is about. Your notebook provides an incredible learning experience about both the asked and unasked questions with wonderful patience. – kalmdown Sep 02 '21 at 19:53

3 Answers3

3

You can map the array to an array of the two roles and flatten it in the same step with flatMap, then remove duplicates by making it into a Set and back into an array:

roles = [...new Set(data.flatMap((d) => [d.initiatorRole, d.receiverRole]))]

That returns ["dispatch", "police", "fire"]. Here’s a demo in an Observable notebook.

Toph
  • 2,561
  • 2
  • 24
  • 28
0

You can use find method to find whether it is exist in result based on that you can push or ignore it

const myData = [
  {initiatorRole: "dispatch", receiverRole: "police"},
  {initiatorRole: "dispatch",  receiverRole: "fire"},
  {initiatorRole: "police", receiverRole: "dispatch"}
  ]

function flatAndRemoveDuplicate(data) {
  const result = [];
  
  for(let role of data) {
    const canfindInitiatorInResult = result.find(item => item.role == role.initiatorRole)
    const canfindReceiverResult = result.find(item => item.role == role.receiverRole)
     
    if(!canfindInitiatorInResult){
      result.push({role: role.initiatorRole})  
    }
    if(!canfindReceiverResult) {
            result.push({role: role.receiverRole})  
    }
  }
  return result;
}

console.log(flatAndRemoveDuplicate(myData))
   
Indraraj26
  • 1,726
  • 1
  • 14
  • 29
0
data = [
  { initiatorRole: "dispatch", receiverRole: "police" },
  { initiatorRole: "dispatch", receiverRole: "fire" },
  { initiatorRole: "police", receiverRole: "dispatch" },
]

let rObj = []
data.forEach((d) => {
  rObj.push({ role: d.initiatorRole })
  rObj.push({ role: d.receiverRole })
})

const duplicateRemover = new Set()

const distinctArrObj = rObj.filter((obj) => {
  if (duplicateRemover.has(JSON.stringify(obj))) return false
  duplicateRemover.add(JSON.stringify(obj))
  return true
})

console.log(distinctArrObj)