【发布时间】:2022-12-01 19:20:34
【问题描述】:
I have a nested object and an array of objects (http://jsfiddle.net/9phkbgqe/):
let data1 =
{
"fields": {
"Main": {
"Personal Details": {
"Surname": "Smith",
"Forename1": "John",
"Nickname": "Johny",
"Gender": "Male",
"Date_of_Birth": "05/04/1979",
"Marital_Status": "Divorced"
}
}
}
}
let data2 = [
{
"name": "Surname",
"displayName": "Surname",
"value": "Bush",
"dataType": "STRING",
"displayLevel1": "Main",
"displayLevel2": "Personal Details",
"displayLevel3": ""
}
]
data2is the original data source in this scenario.
So, indata2I want to use the key name use its value, in this example its "surname". Then indata1find the value of "surname", in this example that's "smith". I then want to use "smith" as the new value for the value key back indata2- which replaces "Bush" in this example.
End result being:
let data2 = [
{
"name": "Surname",
"displayName": "Surname",
"value": "Smith",
"dataType": "STRING",
"displayLevel1": "Main",
"displayLevel2": "Personal Details",
"displayLevel3": ""
}
]
Any help would be appreciated here! thanks
【问题讨论】:
-
Are you expecting this - data2.value = data1.fields[data2.displayLevel1][data2.displayLevel2][data2.name] ??
-
no, the expectation is the example shown above. let data2 = [ { "name": "Surname", "displayName": "Surname", "value": "Smith", "dataType": "STRING", "displayLevel1": "Main", "displayLevel2": "Personal Details", "displayLevel3": "" } ]
-
the only value that has changed is the value of the value key in data2
标签: javascript arrays object mapping