LeeR LeeR - 2 months ago 6
Javascript Question

Added a Javascript object to another Javascript object

Alright, so this is doing my head in and I don't know what to do. I have the following two objects

records = {A: {subdomain: "testing", ip_address: "222.222.222.22"}}
thisRecord = {subdomain: "test", ip_address: "111.111.111.111"}


I'm Trying to add
thisRecord
to the end of
records.A
.

It seems rather simple, but I can't for the life of me figure this out.

Help... Please!

Answer

JSFiddle

You should structure your records.A object as an array of objects:

records = {
  A: [
       {
         subdomain: "testing",
         ip_address: "222.222.222.22"
       }
  ]
}

Then, whenever you want to append new data, simply push it onto records.A

thisRecord = {subdomain: "test", ip_address: "111.111.111.111"}

// Append your new record to `records.A`
records.A.push(thisRecord);

Your records.A will then look like:

records = {
  A: [
       {
         subdomain: "testing",
         ip_address: "222.222.222.22"
       },
       {
         subdomain: "test",
         ip_address: "111.111.111.111"
       }
  ]
}