0

I would like to merge two objects into one which have same property name. For example,

var a = {
            "user-activity":{
                "name":"INPUT - radio",
                "value":"radio - No"
            }
        };
var b = {
            "user-activity":{
                "name":"INPUT - radio",
                "value":"radio - No"
            }
        };
var c = a.concat(b);

I am expecting output like below.

{
    "user-activity":{
                        "name":"INPUT - radio",
                        "value":"radio - No"
                    },
    "user-activity":{
                        "name":"INPUT - radio",
                        "value":"radio - No"
                    }
};

And not like below.

[{
     "user-activity":{
                        "name":"INPUT - radio",
                        "value":"radio - No"
                        }
},{
     "user-activity":{
                        "name":"INPUT - radio",
                        "value":"radio - No"
                        }
}]
JJJ
  • 32,902
  • 20
  • 89
  • 102
Green Computers
  • 723
  • 4
  • 14
  • 24

2 Answers2

0

Having duplicate property names in an object is in no way possible. Probably you need to rethink the scenario where you need to have a solution like this and figure out some alternate solution.

Amit Chawla
  • 44
  • 1
  • 6
0

Keys are like Primary Keys / Unique IDs in database to draw an analogy - it needs to be unique because of the way it is implemented. Keys are the important thing, which identifies a value. All languages that have hash table data structures it will be like this only.

Also in your use case, I guess you need to merge them, it is the same value.

You can also check this How can I merge properties of two JavaScript objects dynamically?

Nishant
  • 20,354
  • 18
  • 69
  • 101