用对象中的新值替换数组中的重复项
我正在尝试“平坦”一个具有多个重复值的数组。该数组是由CSV创建的,然后我尝试运行API请求。 CSV的格式如下:
Posts | Comments |
post1 comment1
post1 comment2
post1 comment3
post2 comment1
post2 comment2
post2 comment3
我使用的是返回的papaparse:
[{post:'post1',注释:'comment1'},{post:'post1',注释:'comment2'},。 。
]
我尝试使用.map
并引用索引
检查是否与当前post
相同>发布如果是.push
到上一个索引,我无法使用.map .map
进行
此操作的正确方法是什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
地图
其中post
是键,该值是一个具有post
和分组注释
post
的数组映射#值
,您可以获取分组对象的列表Array#reduce
, iterate over the list while updating aMap
where thePost
is the key and the value is an object withPost
and groupedComments
array for thePost
Map#values
, you can get a list of grouped objects