Home > Net >  Use a keys value to find a matching key in another object and replace with its value
Use a keys value to find a matching key in another object and replace with its value

Time:12-01

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"
          },
          "More Details": {
            "Injury": "Hand",
          }
        }
      }
    }
    
let data2 = [
      {
        "name": "Surname",
        "displayName": "Surname",
        "value": "Bush",
        "dataType": "STRING",
        "displayLevel1": "Main",
        "displayLevel2": "Personal Details",
        "displayLevel3": ""
      },
      {
        "name": "Injury",
        "displayName": "Injury",
        "value": "Arm",
        "dataType": "STRING",
        "displayLevel1": "Main",
        "displayLevel2": "More Details",
        "displayLevel3": ""
      }
    ]

data2 is the original data source in this scenario.

So, in data2 I want to use the key name use its value, in this example its "Surname". Then in data1 find the value of "Injury", in this example that's "smith". I then want to use "smith" as the new value for the value key back in data2 - which replaces "Bush" in this example.

Another example added So, in data2 I want to use the key name use its value, in this example its "Injury". Then in data1 find the value of "Injury", in this example that's "Hand". I then want to use "Hand" as the new value for the value key back in data2 - which replaces "Arm" in this example.

End result being:

let data2 = [
      {
        "name": "Surname",
        "displayName": "Surname",
        "value": "Smith",
        "dataType": "STRING",
        "displayLevel1": "Main",
        "displayLevel2": "Personal Details",
        "displayLevel3": ""
      },
      {
        "name": "Injury",
        "displayName": "Injury",
        "value": "Hand",
        "dataType": "STRING",
        "displayLevel1": "Main",
        "displayLevel2": "More Details",
        "displayLevel3": ""
      }
    ]

Any help would be appreciated here! thanks

CodePudding user response:

const newData = data2.map((data) => {
  const val = ''
  if (data.displayLevel3) {
    return {
      ...data,
      value: data1.fields[data.displayLevel1][data.displayLevel2][data.displayLevel3][data.name]
    }
  }
  if (data.displayLevel2) {
    return {
      ...data,
      value: data1.fields[data.displayLevel1][data.displayLevel2][data.name]
    }
  }
  if (data.displayLevel1) {
    return {
      ...data,
      value: data1.fields[data.displayLevel1][data.name]
    }
  }
  return {
    ...data,
    value: data1.fields[data.name]
  }
})

CodePudding user response:

A very basic way of doing it would be

// get the value of key "Name" in data 2
const nameVal = data2[0]['name'] 

// once you have the value you can get the surname from data 1 obj 
const simplifiedObj = data1['fields']['Main']['Personal Details']
const surname = simplifiedObj[nameVal]

//finally you can assign your data2 (which is an array of objects, and not an object itself) the new value
data2[0]['value'] = surname
  • Related