-1

Can any one please help me how to add objects to another array of Objects

myArray = [

        {
            "A" :{
                values  
                },
            "B" :{
                values
                },
             "C":{
                 values
             }
         }

]

another Object:

 {
      "D":{

         values 
        },
      "E":{
         values 
      }

    }

I want to add next objects like D and E to My Array of First Object.

it shuold be like this

[
    { 

   "A":{},
   "B":{},
   "C":{},
   "D":{}, 
   "E":{}
}
]

Cna you help me any one how to add this objects

Thanks in Advance

JJJ
  • 32,902
  • 20
  • 89
  • 102
Siva
  • 181
  • 1
  • 1
  • 6
  • 1
    Why do you have this in an array? Won't a single object can hold all these objects as children? – thefourtheye May 14 '15 at 05:57
  • objectname={content}; myArray.push(objectname); – Arunprasanth K V May 14 '15 at 05:59
  • Hi @ Arunprasanth, can you please explain clearly if don't mine. Thanks – Siva May 14 '15 at 06:00
  • possible duplicate of [How can I merge properties of two JavaScript objects dynamically?](http://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically) – JJJ May 14 '15 at 06:03

2 Answers2

0

try something like this

myarray=[{"A":"e1"},{"B":"e2"},{"C":"e4"}];
      var obj={"D":"e5"};                
       myarray.push(obj);
          alert(myarray[3].D)

will alert e5

DEMO

Update :

myarray=[{"A":"e1"},{"B":"e2"},{"C":"e4"}];
var obj={"D":"e6","E":"e9"};                
       myarray.push(obj);
          alert(myarray[3].F)

will alert e9

Arunprasanth K V
  • 20,733
  • 8
  • 41
  • 71
0

According to my understanding you have one array of object i.e.

myArray = [{"A" :{},"B" :{},"C":{}}] 

and you want to add some property in that object so just use

myArray[0].D={};
myArray[0].E={};

console.log(myArray[0]);

And if You want to add more objects in Array use push method

var obj={"A1" :{},"B1" :{},"C1":{}} 

if you want to add in myArray then use

myArray.push(obj);

yours array of object will be

myArray = [{"A" :{},"B" :{},"C":{}},
           {"A1" :{},"B1" :{},"C1":{}}] 

Hope it clears yours doubt.

Roli Agrawal
  • 2,356
  • 3
  • 23
  • 28