I'm trying to "flatten" an array that has multiple duplicate values. The array is created from a CSV that I'm then trying to run API requests on. The format of the CSV is as follows:
Posts | Comments |
post1 comment1
post1 comment2
post1 comment3
post2 comment1
post2 comment2
post2 comment3
I'm using Papaparse which returns:
[{Post: 'post1', Comment: 'comment1'}, {Post: 'post1', Comment: 'comment2'}, ...]
So I thought I would try to flatten them to where it would look like:
[{Post: 'post1', {Comment: 'comment1'}, {Comment: 'comment2'}}]
I tried using a .map and referencing the index to check if the previous Post was the same as the current Post if it is then .push to the previous index which I can't do using .map
What would be the correct way of doing this?
[ { "Post": "post1", "Comments": [ "comment1", "comment2", "comment3" ] } ... ]