如何为每个通过.map的同一个数组创建两个相同的对象?
有一个如下的数据集:
data = [
{initiatorRole: "dispatch", receiverRole: "police"},
{initiatorRole: "dispatch", receiverRole: "fire"},
{initiatorRole: "police", receiverRole: "dispatch"}
]
我想将这些放入一组对象中,我将在其中删除重复项并添加其他数据。
roles = [
{role: "dispatch"},
{role: "police"},
{role: "dispatch",
{role: "fire"},
{role: "police"},
{role: "dispatch"}
]
我目前拥有的代码:
roles = data.map(d => {
let rObj = {}
rObj["id"] = d.initiatorRole
rObj["id"] = d.receiverRole
return rObj;
});
但是当然这只是返回最后一个 rObj 集。这可以通过一个地图调用来完成吗?