将一个 Javascript 对象添加到另一个 Javascript 对象
Added a Javascript object to another Javascript object
好吧,这让我很头疼,我不知道该怎么办。我有以下两个对象
records = {A: {subdomain: "testing", ip_address: "222.222.222.22"}}
thisRecord = {subdomain: "test", ip_address: "111.111.111.111"}
我正在尝试将 thisRecord
添加到 records.A
的末尾。
这看起来很简单,但我这辈子都想不通。
求助...拜托!
如果我理解正确,那么 records["A"] 需要是一个数组来保存这两个对象。假设是这种情况,你可以做这样的事情
newRecord = {}
newRecord.A = []
newRecord.A.push(records.A);
newRecord.A.push(thisRecord);
records = newRecord;
您应该将 records.A
对象构造为对象数组:
records = {
A: [
{
subdomain: "testing",
ip_address: "222.222.222.22"
}
]
}
然后,每当你想添加新数据时,只需将其推送到 records.A
thisRecord = {subdomain: "test", ip_address: "111.111.111.111"}
// Append your new record to `records.A`
records.A.push(thisRecord);
您的 records.A
将如下所示:
records = {
A: [
{
subdomain: "testing",
ip_address: "222.222.222.22"
},
{
subdomain: "test",
ip_address: "111.111.111.111"
}
]
}
好吧,这让我很头疼,我不知道该怎么办。我有以下两个对象
records = {A: {subdomain: "testing", ip_address: "222.222.222.22"}}
thisRecord = {subdomain: "test", ip_address: "111.111.111.111"}
我正在尝试将 thisRecord
添加到 records.A
的末尾。
这看起来很简单,但我这辈子都想不通。
求助...拜托!
如果我理解正确,那么 records["A"] 需要是一个数组来保存这两个对象。假设是这种情况,你可以做这样的事情
newRecord = {}
newRecord.A = []
newRecord.A.push(records.A);
newRecord.A.push(thisRecord);
records = newRecord;
您应该将 records.A
对象构造为对象数组:
records = {
A: [
{
subdomain: "testing",
ip_address: "222.222.222.22"
}
]
}
然后,每当你想添加新数据时,只需将其推送到 records.A
thisRecord = {subdomain: "test", ip_address: "111.111.111.111"}
// Append your new record to `records.A`
records.A.push(thisRecord);
您的 records.A
将如下所示:
records = {
A: [
{
subdomain: "testing",
ip_address: "222.222.222.22"
},
{
subdomain: "test",
ip_address: "111.111.111.111"
}
]
}