Thursday, September 4, 2025
HomeUncategorisedHow to merge properties of two JavaScript objects dynamically?

How to merge properties of two JavaScript objects dynamically?

Using Spread Operator: Spread operator allows an iterable to expand in places where 0+ arguments are expected. It is mostly used in the variable array where there is more than 1 values are expected. It allows the privilege to obtain a list of parameters from an array.

Javascript objects are key-value paired dictionaries. We can merge different objects into one using the spread (…) operator.
Syntax:

object1 = {...object2, ...object3, ... }

Example 1:




<script>
let A = {
    name: "neveropen",
};
  
let B = {
    domain: "https://geeksforgeeks.org"
};
  
let Sites = { ...A, ...B };
  
console.log(Sites)
</script>


Output:

Example 2: Suppose the objects have the same keys. In this case, the value of the key of the object which appears later in the distribution is used.




<script>
let A = {
    name: "neveropen",
};
  
let B = {
    name: "wordpress"
};
  
let Sites = { ...A, ...B };
  
console.log(Sites)
</script>


Output:

Another method: We can also use the Object.assign() method to merge different objects.

Example:




<script>
let A = {
  name: "neveropen",
};
  
let B = {
  domain: "https://geeksforgeeks.org"
};
  
let Sites = Object.assign(A, B);
  
console.log(Sites);
</script>


Output:

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32261 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6695 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS