0

Hi Iam new to underscore utility can some one help me out on this.

Sample JSON:

[
  {
    L0: "Level0",
    L1: "Level1",
    L2: "Level2",
    L3: "Level3"
  },
  {
    L0: "Level0",
    L1: "Level1a",
    L2: "Level2",
    L3: "Level3"
  },
  {
    L0: "Level0",
    L1: "Level1",
    L2: "Level2a",
    L3: "Level3"
  },
  {
    L0: "Level0",
    L1: "Level1",
    L2: "Level2",
    L3: "Level3a"
  },
  {
    L0: "Level0a",
    L1: "Level1",
    L2: "Level2",
    L3: "Level3"
  }
];

Expected JSON:

[
  {
    L0: "Level0",
    L1: [
      {
        L1: "Level1",
        L2: [
          {
            L2: "Level2",
            L3: [
              {
                L3: "Level3"
              },
              {
                L3: "Level3a"
              }
            ]
          },
          {
            L2: "Level2a",
            L3: "Level3"
          }
        ]
      },
      {
        L1: "Level1a",
        L2: "Level2",
        L3: "Level3"
      }
    ]
  },
  {
    L0: "Level0a",
    L1: "Level1",
    L2: "Level2",
    L3: "Level3"
  }
];

Please help me out in achieving the expected JSON format through underscore/lodash. Its becoming a nightmare for me to achieve the same. I wrote lengthy codes still could not achieve it. Appreciate your help

Thanks in advance!

Taha Md
  • 21
  • 2
  • 5
  • 3
    Please post a [minimum, complete, verifiable](https://stackoverflow.com/help/mcve) example of what you've tried so far – wbadart Apr 10 '18 at 14:15
  • Please read [What is the difference between JSON and Object Literal Notation?](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation) – str Apr 10 '18 at 14:17
  • 3
    plus please explain what `Expected JSON` is. I cannot find a rule. – apple apple Apr 10 '18 at 14:17
  • I could not achieve multi level nesting. Iam completely new to this. help is appreciated. – Taha Md Apr 10 '18 at 14:17

1 Answers1

0

For this tree structure, you need an array with the relevant keys, a loop for array of objects and a loop for the properties with short circuit, if no more nested items are needed.

You could look for a wanted property of the level and if found, check if the property is an array, if not create one and return that array, continue.

If not found, create a new object with the actual and rest of keys and stop iteration.

For getting a JSON compliant string, you might use JSON.stringify, which is not included in this answer.

var array = [{ L0: "Level0", L1: "Level1", L2: "Level2", L3: "Level3" }, { L0: "Level0", L1: "Level1a", L2: "Level2", L3: "Level3" }, { L0: "Level0", L1: "Level1", L2: "Level2a", L3: "Level3" }, { L0: "Level0", L1: "Level1", L2: "Level2", L3: "Level3a" }, { L0: "Level0a", L1: "Level1", L2: "Level2", L3: "Level3" }],
    keys = ['L0', 'L1', 'L2', 'L3'],
    result = [];

array.forEach(o => {
    var level = result;
    keys.some((k, i, kk) => {
        var temp = level.find(l => l[k] === o[k]);

        if (temp && kk[i + 1]) {
            if (!Array.isArray(temp[kk[i + 1]])) {
                temp[kk[i + 1]] = [Object.assign(...kk.slice(i + 1).map(l => ({ [l]: temp[l] })))];
                kk.slice(i + 2).forEach(l => { delete temp[l]; });
            }
            level = temp[kk[i + 1]];
            return;
        }
        level.push(Object.assign(...kk.slice(i).map(l => ({ [l]: o[l] }))));
        return true;
    }, result);
});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392