title: Restructure JSON array
body: <p>I'm trying to restructure the JSON array in following manner. In the output, I need <code>id</code> as key and object itself as it's value.</p>
  
  <p>Sample input:</p>
  
  <pre><code>[
      {
          "id": "1",
          "children": [
              {
                  "id": "1-1",
                  "children": [
                      {
                          "id": "1-1-1",
                          "children": []
                      },
                      {
                          "id": "1-1-2",
                          "children": []
                      }
                  ]
              },
              {
                  "id": "1-2",
                  "children": []
              }
          ]
      },
      {
          "id": "2",
          "children": []
      },
      {
          "id": "3",
          "children": [
              {
                  "id": "3-1",
                  "children": []
              }
          ]
      }
  ]
  </code></pre>
  
  <p>Required output:</p>
  
  <pre><code>{
      "1": {
          "id": "1",
          "children": {
              "1-1": {
                  "id": "1-1",
                  "children": {
                      "1-1-1": {
                          "id": "1-1-1",
                          "children": []
                      },
                      "1-1-2": {
                          "id": "1-1-2",
                          "children": []
                      }
                  }
              },
              "1-2": {
                  "id": "1-2",
                  "children": []
              }
          }
      },
      "2": {
          "id": "2",
          "children": []
      },
      "3": {
          "id": "3",
          "children": {
              "3-1": {
                  "id": "3-1",
                  "children": []
              }
          }
      }
  }
  </code></pre>
  
  <p>The following code gives me almost the required answer.</p>
  
  <pre><code>function restruct(arr) {
      var newArray = arr.map(function(obj) {
          var t = {};
          if (obj.children)
              obj.children = restruct(obj.children);
          t[obj.id] = obj;
          return t;
      });
      return newArray;
  }
  </code></pre>
  
  <p>The output is:</p>
  
  <pre><code>[
      {
          "1": {
              "id": "1",
              "children": [
                  {
                      "1-1": {
                          "id": "1-1",
                          "children": [
                              {
                                  "1-1-1": {
                                      "id": "1-1-1",
                                      "children": []
                                  }
                              },
                              {
                                  "1-1-2": {
                                      "id": "1-1-2",
                                      "children": []
                                  }
                              }
                          ]
                      }
                  },
                  {
                      "1-2": {
                          "id": "1-2",
                          "children": []
                      }
                  }
              ]
          }
      },
      {
          "2": {
              "id": "2",
              "children": []
          }
      },
      {
          "3": {
              "id": "3",
              "children": [
                  {
                      "3-1": {
                          "id": "3-1",
                          "children": []
                      }
                  }
              ]
          }
      }
  ]
  </code></pre>
  
  <p>If you notice, everything is as per expected output except the <code>children</code> nodes. It returns array of objects while I need object with key-value pairs. Can anybody help me with this?</p>