How to match nested object keys with values from array with matching keys in lodash

324 views Asked by At

Sorry for the confusing title. I have the results of a form generated from the ngx-formly library. They are an object with key and value pairs that look exactly like this:

Form Feedback

{
    "zero": {
        "a": false,
        "feedback": {
            "a": false
        }
    },
    "feedbackConsent": false
}

and then I have a "form" array that looks like this:

Form Questions

[
  {
    "key": "zero",
    "fieldGroup": [
      {
        "key": "a",
        "templateOptions": {
          "label": "Do you own a car?"
        }
      },
      {
        "key": "feedback",
        "fieldGroup": [
          {
            "key": "a",
            "templateOptions": {
              "label": "Do you find this question helpful?"
            }
          }
        ]
      },
    ]
  },
  {
    "key": "feedbackConsent",
    "templateOptions": {
      "label": "Do you agree to provide feedback?"
    }
  }
]

Pay attention to how the "feedback" keys align with the keys in the form. What I want to do is simply generate a new object where the feedback object keys are replaced by the "labels" from the array (under template options).

So it would look like this:

Desired Output

{
  "zero": {
    "Do you own a car?": false,
    "feedback": {
      "Do you find this question helpful?": false
    }
  },
  "Do you agree to provide feedback?": false
}

I seem to recall doing something similar with Lodash but cannot remember how, can you please help me?

1

There are 1 answers

0
Md. Sujan On

I think you should create a json file for this code after that fetch the json file within a react hook and then use react form hook for load the feadback