title: Map Json Data to new key-value pair JavaScript object
body: <p>I want to map one json data to a new javascript object like following. Here the json data is dynamic and can have more files with more users. The group information is new and it depends on parent-child information. Can anyone please help me out? Thank you for your time.</p>
  
  <p>Before: </p>
  
  <pre><code>{
    "userinfo": {
      "/home/user/main/sub/info/1stfile.txt": {
        "John": "something",
        "Mike": "something",
        "Merry": "something",
        "Susan": "something"
      },
      "/home/user/main/info/2ndfile.txt": {
        "Mulan": "something",
        "James": "something"
      },
      "/home/user/main/info/3rdfile.txt": {
        "Nancy": "something"
      },
      "/home/user/main/4thfile.txt": {
        "Kamal": "something",
        "Xian": "something",
        "Mila": "something"
      }
    }
  }
  </code></pre>
  
  <p>After: </p>
  
  <pre><code>{
    "name": "main",
    "children": [
      {
        "name": "1stfile",
        "children": [
          {
            "name": "John",
            "group": "1"
          },
          {
            "name": "Mike",
            "group": "1"
          },
          {
            "name": "Merry",
            "group": "1"
          },
          {
            "name": "Susan",
            "group": "1"
          }
        ],
        "group": 1
      },
      {
        "name": "2ndfile",
        "children": [
          {
            "name": "Mulan",
            "group": 2
          },
          {
            "name": "James",
            "group": 2
          }
        ],
        "group": 2
      },
      {
        "name": "3rdfile",
        "children": [
          {
            "name": "Nancy",
            "group": 3
          }
        ],
        "group": 3
      },
      {
        "name": "4thfile",
        "children": [
          {
            "name": "Kamal",
            "group": 4
          },
          {
            "name": "Xian",
            "group": 4
          },
          {
            "name": "Mila",
            "group": 4
          }
        ],
        "group": 4
      }
    ],
    "group": 0
  }
  </code></pre>
  
  <p>I was trying to build one block of parent-child by using following code</p>
  
  <pre class="lang-js prettyprint-override"><code>var jsonData = json["userinfo"];
  var keys = Object.keys(jsonData);
  console.log(keys);
  let data = {};
  for (var j = 0; j &lt; keys.length; j++) {
    let g = 1;
    data[j] = { name: keys[j], group: g++ };
  }
  console.log(data);
  </code></pre>
  
  <p>Which is giving following output</p>
  
  <pre><code>{
    0: {
      "name": "/home/user/main/sub/info/1stfile.txt",
      "group": 1
    },
    1: {
      "name": "/home/user/main/info/2ndfile.txt",
      "group": 1
    },
    2: {
      "name": "/home/user/main/info/3rdfile.txt",
      "group": 1
    },
    3: {
      "name": "/home/user/main/4thfile.txt",
      "group": 1
    }
  }
  </code></pre>
  
  <p>The value is assigning properly but is creating extra keys (0,1,2,3)!</p>