📅  最后修改于: 2022-03-11 15:03:14.648000             🧑  作者: Mango
var users = [{userId: "1", name: 'harald'}, {userId: "2", name: 'jamie'}];
var obj = {};
users.forEach(user => {
obj = {
...obj,
[user.userId]: user,
}
})
console.log(obj)
// {
// 1: {userId: "1", name: "harald"}
// 2: {userId: "2", name: "jamie"}
// }