【发布时间】:2019-09-17 21:50:34
【问题描述】:
我有一个序列化模型,它是一个对象数组(关联?),我需要在其中获取和设置特定字段。
我尝试过使用 jQuery 和 js 进行迭代,但取得了一些有限的成功,但只能获得 1 级键。
{
"Branch": {
"ID": 123,
"Code": "xyz",
"FkRegionID": null,
"FkEntityPersonID": null,
"ParentID": null,
"Detail": null,
"Addresses": [],
"TelephoneNumbers": [],
"DeletedTelephoneNumbers": [],
"BankAccounts": [],
},
"ParentLookup": null,
"Address": {
"ID": 55,
"FkEntityPersonID": 27,
"FkEntityAddressTypeID": 1,
"Address1": null,
"Address2": null,
"Address3": null,
"Address4": null,
"FkCityID": null,
"PostalCode": null,
"CountryID": null,
"RegionID": null,
"AddressTypeDetail": null,
"CityDetail": null
},
"AddressCityLookup": null,
"Telephone": {
"ID": null,
"FkEntityPersonID": 27,
"FkTelephoneTypeID": 1,
"TelephoneNumber": 0826559999,
"TelephoneTypeDetail": null
},
"TelephoneTypeLookup": null,
}
我想获取任何键值对的值并进行设置。例如获取 ID=123 的 'BranchCode' 和 'code' 并设置 'Code' 字段。
编辑: 这行得通。 下一步是将其提取到它自己的数组中,但这是一个不同的问题。
$.each(serializedObject, function (key, value)
{
console.log("key= " + key + " ." + value);
if (key == 'Branch')
{
value.ID = 456;
// Get this into a standalone array?
// newArray
}
});
谢谢
【问题讨论】:
-
您尝试获取或设置对象的键是什么?您面临什么问题?
标签: javascript associative-array key-value