Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

js create object with keys

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"}
// }
Source by developer.mozilla.org #
 
PREVIOUS NEXT
Tagged: #js #create #object #keys
ADD COMMENT
Topic
Name
9+6 =