1

I have the following problem. I'd like to convert an array of strings in dot notation to a nested json object.

example input: obj = {"project1.foo.a" : "value", "project1.foo.b" : "value","project1.bar" : "value", "project2.foo.a" : "value", "project2.foo.b" : "value", "project2.foo.c" : "value", "project2.foo.bar" : "value"};
desired output: 

{
  "id": 0,
  "title": "Projects",
  "has_children": 1,
  "level": 1,
  "value":"string",
  "children": [
    {
      "id": 1,
      "title": "Project 1",
      "has_children": true,
      "level": 2,
      "value":"string",
      "children": [
        {
          "id": 11,
          "title": "foo",
          "has_children": true,
          "level": 3,
          "value":"string",
          "children": [
            {
              "id": 111,
              "title": "a",
              "has_children": false,
              "level": 4,
              "value":"string",
              "children": [

              ]
            },
            {
              "id": 112,
              "title": "b",
              "has_children": false,
              "level": 4,
              "value":"string",
              "children": [

              ]
            }
          ]
        },
        {
          "id": 12,
          "title": "bar",
          "has_children": false,
          "level": 3,
          "value":"string",
          "children": [

          ]
        }
      ]
    },
    {
      "id": 2,
      "title": "Project 2",
      "has_children": true,
      "level": 2,
      "value":"string",
      "children": [
        {
          "id": 21,
          "title": "foo",
          "has_children": true,
          "level": 3,
          "value":"string",
          "children": [
            {
              "id": 211,
              "title": "a",
              "has_children": false,
              "level": 4,
              "value":"string",
              "children": [

              ]
            },
            {
              "id": 212,
              "title": "b",
              "has_children": false,
              "level": 4,
              "value":"string",
              "children": [

              ]
            },
            {
              "id": 213,
              "title": "c",
              "has_children": false,
              "level": 4,
              "value":"string",
              "children": [

              ]
            }
          ]
        },
        {
          "id": 22,
          "title": "bar",
          "has_children": false,
          "level": 3, 
          "value":"string",
          "children": [

          ]
        }
      ]
    }
  ]
}

There are great functions to unflatten the example object, e. g. with lodash, but I'm not able to add the descendants recursively into the childrens array.

example code:
const unflatten = (flattedObject) => {
let result = {};
_.keys(flattedObject).forEach(function (key, value){    
_.set(result, key, flattedObject[key]);     
})
return result;
}
console.log(unflatten(obj));

Maybe someone knows a good/neat or at least working approach to do this. :D I want to feed this plugin with the data https://travistidwell.com/jquery.treeselect.js/. Related posts: How to populate jquery treeselect widget?

How to unflatten a JavaScript object in a daisy-chain/dot notation into an object with nested objects and arrays?

Xyz753
  • 11
  • 2

0 Answers0