I am kind of new in programming world, please help me. Any help would be appreciated!
I have an array with data like shown below. I have to make the data so that in every group only contains paired user_id
in sequence. The paired user_id
should be different if they are going to be paired.
[
{"user_id": 35, "group": "A"},
{"user_id": 40, "group": "B"},
{"user_id": 39, "group": "B"},
{"user_id": 39, "group": "B"},
{"user_id": 39, "group": "B"},
{"user_id": 40, "group": "B"},
{"user_id": 39, "group": "B"},
{"user_id": 39, "group": "B"},
{"user_id": 40, "group": "C"},
{"user_id": 39, "group": "C"}
]
So the data will be like this:
The first item is removed because it doesn't has pair.
The 5th and 6th items are removed because it has same user_ids with each other (user_id
should be different if it is going to be paired).
[
{"user_id": 35, "group": "A"}, <-- remove
{"user_id": 40, "group": "B"},
{"user_id": 39, "group": "B"},
{"user_id": 39, "group": "B"}, <-- remove
{"user_id": 39, "group": "B"}, <-- remove
{"user_id": 40, "group": "B"},
{"user_id": 39, "group": "B"},
{"user_id": 39, "group": "B"}, <-- remove
{"user_id": 40, "group": "B"}, <-- remove
{"user_id": 40, "group": "C"},
{"user_id": 39, "group": "C"}
]
So, the result would be:
[
{"user_id": 40, "group": "B"},
{"user_id": 39, "group": "B"},
{"user_id": 40, "group": "B"},
{"user_id": 39, "group": "B"},
{"user_id": 40, "group": "C"},
{"user_id": 39, "group": "C"}
]
What is the best way to achieve this (using splice method or create a new array and then push it, or another ways)?