我有两个数组。例如这样的东西:
"results": [
{
"seen": false,
"_id": "5f6a9608c248df5c14a6aa53",
"user_id": "5f69264fe410854be4840e9f",
"invited_user_id": "5f69254ee410854be4840e99",
"createdAt": "2020-09-23T00:25:44.314Z",
"__v": 0
},
{
"seen": false,
"_id": "5f6a943d8d745b422cd67796",
"user_id": "5f69264fe410854be4840e9f",
"invited_user_id": "5f69254ee410854be4840e99",
"createdAt": "2020-09-23T00:18:05.634Z",
"__v": 0
}
],
"users": [
{
"_id": "5f69254ee410854be4840e99",
"name": "Paweł",
"surname": "mnv"
},
{
"_id": "5f692626e410854be4840e9c",
"name": "Sebastian",
"surname": "fasd"
}
]
现在,当results.invited_user_id === users._id时,我想用用户数组中的用户对象替换“ invited_user_id”表单结果数组。
我想得到什么:
"results": [
{
"seen": false,
"_id": "5f6a9608c248df5c14a6aa53",
"user_id": "5f69264fe410854be4840e9f",
"invited_user_id": {
"_id": "5f69254ee410854be4840e99",
"name": "Paweł",
"surname": "mnv"
},
"createdAt": "2020-09-23T00:25:44.314Z",
"__v": 0
},
{
"seen": false,
"_id": "5f6a943d8d745b422cd67796",
"user_id": "5f69264fe410854be4840e9f",
"invited_user_id": {
"_id": "5f692626e410854be4840e9c",
"name": "Sebastian",
"surname": "fasd"
},
"createdAt": "2020-09-23T00:18:05.634Z",
"__v": 0
}
]
我可以写一些长函数来做到这一点,但是我想知道是否有更快的方法吗?
答案 0 :(得分:1)
是的......
sorted
现在finalData拥有您想要的JSON。