Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

How to Concatenate or Merge Objects With the Spread Operator in JavaScript

let userName = { name: "John Doe" };
let userSex = { sex: "Male" };

let user = { ...userName, ...userSex };

console.log(user); // { name: "John Doe", sex: "Male" }

// Note: In a situation where one key has another property,
// the last property overrides the first instance:

let userName = { name: "John Doe" };
let userSex = { sex: "Female", name: "Jane Doe" };

let user = { ...userName, ...userSex }; // { name: "Jane Doe", sex: "Female" }
Source by www.freecodecamp.org #
 
PREVIOUS NEXT
Tagged: #How #Concatenate #Merge #Objects #With #Spread #Operator #JavaScript
ADD COMMENT
Topic
Name
9+3 =