gpt4 book ai didi

javascript - 带有映射的javascript中以特定格式显示的数组

转载 作者:行者123 更新时间:2023-12-02 07:18:07 24 4
gpt4 key购买 nike

需要以特定格式显示数组。输入数组:

let userList = [
{"id": "12356","username": "test@gmail.com"},
{"id": "333333", "username": "test1@gmail.com"}
]
let userArray = [
{"username": "test@gmail.com","sharedPaper": "some paper"}
]

预期结果:

let resultArr = [
{"id":"12356","username": "test@gmail.com","sharedPaper": "some paper"}
]

最佳答案

您可以使用filtermap 方法(假设username 是唯一的)

const userList = [{
"id": "12356",
"username": "test@gmail.com"
}, {
"id": "333333",
"username": "test1@gmail.com"
}];
const userArray = [{"username": "test@gmail.com","sharedPaper": "some paper"}];

const result = userList
.filter(user => userArray.find(u => u.username === user.username))
.map(user => {
const userAdditionalData = userArray.find(u => u.username === user.username);
return {
...user,
...userAdditionalData
}
});

console.log(result)

关于javascript - 带有映射的javascript中以特定格式显示的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55297911/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com