📜  对象键 javascript 代码示例

📅  最后修改于: 2022-03-11 15:01:58.045000             🧑  作者: Mango

代码示例6
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"}
// }