【发布时间】:2018-05-03 06:14:51
【问题描述】:
我有 4 个单独的对象数组,有没有办法根据对象内的键将它们全部连接成一个大对象。
这是一个例子 输出:我想要达到的目标。
[
{
"bugId": "",
"testerId": "",
"firstName": "",
"lastName": "",
"country": "",
"deviceId":"",
"description":""
}
]
testers的对象(超过500个)
[
{
"testerId":"1",
"firstName":"John",
"lastName":"Doe",
"country":"US",
}
]
bugId 的对象(这应该是我们可以从中获取输出的主要对象)
因为deviceId 连接到description,testerId 连接到firstName,lastName 和Country。
[
{
"bugId":"1",
"deviceId":"1",
"testerId":"1"
}
]
对象为tester_devices,提供1台测试仪4台设备
[
{
"testerId":"1",
"deviceId":"1"
},
{
"testerId":"1",
"deviceId":"2"
},
{
"testerId":"1",
"deviceId":"3"
},
{
"testerId":"1",
"deviceId":"10"
}
]
devices的对象
[
{
"deviceId":"1",
"description":"iPhone 4"
}
]
【问题讨论】:
-
您可以使用 Lodash 的
_.keyBy将您的testers和devices数组转换为对象,并由它们各自的 ID 键入。然后你可以迭代你的bugs数组并建立相关数据 -
tester_devices应该如何参与其中?它是否相关? -
tester_devices,可以避免。由于必须根据设备名称进行搜索,我可以将其替换为数字,即deviceId。
标签: javascript json ecmascript-6 lodash