0

How can I create the MissingUserIdsArray from MainArray in JavaScript?

var MainArray = [
  [
    "10148835*1,2,3,",
    "1,2,3,4,5,"
  ],
  [
    "10148839*4,5,",
    "1,2,3,4,5,"
  ]
];

const MappedArrray = MainArray.map(arr => {
  const SplitID = arr[0].split("*"); 
  return {"AppId": SplitID[0], "AppUserIds": SplitID[1].split(","), "MeetingUserIds": arr[1].split(",")};
});
console.log(MappedArrray);
//My above code is working fine. Just need to make MissingUserIdsArray array in the format I mentioned.

const MissingUserIdsArray = MappedArrray.map(arr => arr.MeetingUserIds.filter(x => !arr.AppUserIds.includes(x)));
console.log(MissingUserIdsArray);

//Need to create MissingUserIdsArray in the following format:
/*
MissingUserIdsArray = [
    {"AppId": 10148835, "MissingUserIds": "4,5"},
    {"AppId": 10148839, "MissingUserIds": "1,2,3"},
]

*/
Karim Ali
  • 2,243
  • 6
  • 23
  • 31
  • duplicate: [How to get the difference between two arrays in JavaScript?](https://stackoverflow.com/questions/1187518/how-to-get-the-difference-between-two-arrays-in-javascript) – pilchard Jan 26 '23 at 19:45

2 Answers2

2

Just split up the strings into a processable format and then exclude one set of ids from the other:

const MainArray = [
  [
    "10148835*1,2,3,",
    "1,2,3,4,5,"
  ],
  [
    "10148839*4,5,",
    "1,2,3,4,5,"
  ]
];

const result = MainArray
  .map( ([s, ids]) => [...s.split('*'), ids.split(',')])
  .map(([AppId, excludes, ids]) => (excludes = excludes.split(','), {AppId, MissingUserIds: ids.filter(id => !excludes.includes(id)).join(',') }) )
console.log(result)
Moritz Ringler
  • 9,772
  • 9
  • 21
  • 34
  • Very smarts, Txs Appreciated. – Karim Ali Jan 26 '23 at 21:02
  • Hey, Can you please explain your statement : .map(([AppId, excludes, ids]) => (excludes = excludes.split(','), {AppId, MissingUserIds: ids.filter(id => !excludes.includes(id)).join(',') }) )? I am more interested in this statement: (excludes = excludes.split(','), {AppId, MissingUserIds: ids.filter(id => !excludes.includes(id)).join(',') }) ) – Karim Ali Jan 27 '23 at 23:05
  • 1
    @KarimAli Pretty sure you are asking about the [Comma Operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator). Let me know if you mean something else – Moritz Ringler Jan 28 '23 at 09:43
  • Txs Appreciated. This is what I was looking for(Comma Operator). – Karim Ali Jan 29 '23 at 18:47
2

There wasn't much missing in your code:

  • The trailing comma in the source data makes me choose match over split
  • Join the array you had to get the comma separated values

var MainArray = [["10148835*1,2,3,","1,2,3,4,5,"],["10148839*4,5,","1,2,3,4,5,"]];

const MappedArrray = MainArray.map(([a, b]) => {
    const [AppId, ...appUserIds] = a.match(/\d+/g);
    const MissingUserIds = b.match(/\d+/g).filter(id => !appUserIds.includes(id)).join();
    return {AppId, MissingUserIds};
});
console.log(MappedArrray);
trincot
  • 317,000
  • 35
  • 244
  • 286