title: Algorithm for merging a strict JS input object into a loose template JS object
body: <p>I have faced a situation where I have two JavaScript objects:</p>
  
  <p>Input object:</p>
  
  <pre><code>{
      name:"a1",
      children:[
          {name:"P", value:'newVal1'},
          {name:"Q", value:'newVal2'},
          {name:"R", value:'newVal3'},
          {name:"S1", children:[
              {name:"T", value:"newVal4"}
          ]},
      ]
  }
  </code></pre>
  
  <p>Template:</p>
  
  <pre><code>{
    name: "a1",
    children: [
      {
        name: "b1",
        children: [
          {
            name: "c1",
            children: [
              {
                name: "P"
              },
            ],
          },
          {
            name: "b2",
            children: [
              {
                name: "c2",
                children: [
                  {
                    name: "Q"
                  },
                ],
              },
              {
                name: "b3",
                children: [
                  {
                    name: "R"
                  },
                ],
              },
              {
                name: "b4",
                children: [
                  ,
                  {
                    name: "c3",
                    children: [
                      ,
                      {
                        name: "s1",
                        children: [
                          {
                            name: "e1",
                            children: [
                              {
                                name: "T"
                              },
                            ],
                          },
                        ],
                      },
                    ],
                  },
                ],
              },
            ],
          },
        ],
      },
    ],
  }
  </code></pre>
  
  <p>I need to merge the input object into the template object such that the new values would be:</p>
  
  <pre><code>{
    name: "a1",
    children: [
      {
        name: "b1",
        children: [
          {
            name: "c1",
            children: [
              {
                name: "P",
                value: "newVal1",
              },
            ],
          },
          {
            name: "b2",
            children: [
              {
                name: "c2",
                children: [
                  {
                    name: "Q",
                    value: "newVal2",
                  },
                ],
              },
              {
                name: "b3",
                children: [
                  {
                    name: "R",
                    value: "newVal3",
                  },
                ],
              },
              {
                name: "b4",
                children: [
                  ,
                  {
                    name: "c3",
                    children: [
                      ,
                      {
                        name: "s1",
                        children: [
                          {
                            name: "e1",
                            children: [
                              {
                                name: "T",
                                value: "newVal4",
                              },
                            ],
                          },
                        ],
                      },
                    ],
                  },
                ],
              },
            ],
          },
        ],
      },
    ],
  }
  </code></pre>
  
  <p>Notice how <code>newVal1</code> etc. values have to land up  in the correct spots in the merged structure.</p>
  
  <p>The intermediate layers (<code>a,b,c</code> etc.) may or may not exist, i.e. there might be many layers in-between before matching the <code>P Q R</code> layers, or on the other side, a given template object may exactly match the structure of the input, and the function/class should still be able to place values inside.</p>
  
  <p>I am using Typescript, but even a JavaScript answer is welcome. </p>
  
  <p>Also, does this sort of object matching have a name?</p>
  
  <p>Really appreciate your time. Thanks.</p>