Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Need to change name of a key in a script that returns a sorted List

I have some code that loops thru a flat json doc and creates a hierarchal of the items based on level and position. It all works as expected but i have the need to change the child elements name from child to items. And thats where my problem starts. There is 2 porions in code where i set the child:

   newparent = {
          ...oldparent,
          child: mItems
        };
      } else {
        newparent = {
          ...oldparent,
          child: [child]

So I tried to change that to items but that does not work, when i change it, I get the items key on the initial items but not for any additional items under that parent.

Here is also link to a working sample Replit Testbed

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My Sample Output

[
  {
    enabled: true,
    guid: 'F56AAC06-D2EB-4E1C-B84D-25F72973312E',
    level: 0,
    name: 'Farms',
    parent: 'F56AAC06-D2EB-4E1C-B84D-25F72973312E',
    position: 0,
    umid: 'F56AAC06-D2EB-4E1C-B84D-25F72973312E',
    child: [
      {
        enabled: true,
        guid: '144C0989-9938-4AEC-8487-094C23A5F150',
        level: 1,
        name: 'New Farm List',
        parent: 'F56AAC06-D2EB-4E1C-B84D-25F72973312E',
        position: 0,
        umid: '144C0989-9938-4AEC-8487-094C23A5F150'
      },
      {
        enabled: true,
        guid: '8FBA7B0B-566E-47CD-885B-1C08B57F34F6',
        level: 1,
        name: 'Farm Lists',
        parent: 'F56AAC06-D2EB-4E1C-B84D-25F72973312E',
        position: 1,
        umid: '8FBA7B0B-566E-47CD-885B-1C08B57F34F6'
      },
      {
        enabled: true,
        guid: 'FCD36DBD-0639-4856-A609-549BB10BEC1A',
        level: 1,
        name: 'Farm Upload',
        parent: 'F56AAC06-D2EB-4E1C-B84D-25F72973312E',
        position: 4,
        umid: 'FCD36DBD-0639-4856-A609-549BB10BEC1A'
      },
      {
        enabled: true,
        guid: '4264DA98-1295-4A65-97C6-313485744B4D',
        level: 1,
        name: 'Campaign',
        parent: 'F56AAC06-D2EB-4E1C-B84D-25F72973312E',
        position: 4,
        umid: '4264DA98-1295-4A65-97C6-313485744B4D',
        child: [
          {
            enabled: true,
            guid: '9CBDC6BB-5B3D-4F53-B846-AFE55F34C1E9',
            level: 2,
            name: 'New Campaign',
            parent: '4264DA98-1295-4A65-97C6-313485744B4D',
            position: 5,
            umid: '9CBDC6BB-5B3D-4F53-B846-AFE55F34C1E9'
          },
          {
            enabled: true,
            guid: '281490B5-C67D-4238-9D52-DE1DFA373418',
            level: 2,
            name: 'Campaign List',
            parent: '4264DA98-1295-4A65-97C6-313485744B4D',
            position: 6,
            umid: '281490B5-C67D-4238-9D52-DE1DFA373418'
          }
        ]
      }
    ]
  }
]

Here is my Code

function menu_sorted(input) {
   
  let max_level = 0;
  var sorted_by_level = {};

  input.forEach(i => {
    if (sorted_by_level.hasOwnProperty("level_" + i.level.toString())) {
      sorted_by_level["level_" + i.level.toString()].push(i);
    } else {
      sorted_by_level["level_" + i.level.toString()] = [i];
      if (i.level > max_level) {
        max_level = i.level;
      }
    }
  });


  for (level = max_level; level > 0; level--) {
    sorted_by_level["level_" + level.toString()].forEach(child => {
      const oldparent = sorted_by_level[
        "level_" + (level - 1).toString()
      ].filter(p => p.guid === child.parent)[0];
      const parentIndex = sorted_by_level[
        "level_" + (level - 1).toString()
      ].findIndex(p => p.guid === child.parent);
      let newparent;

     // delete child.guid;
     // delete child.parent;
      if (oldparent.hasOwnProperty("child") && oldparent.child) {
        var mItems = [...oldparent.child, child];
        mItems.sort((a, b) => (a.position > b.position) ? 1 : -1);
        newparent = {
          ...oldparent,
          child: mItems
        };
      } else {
        newparent = {
          ...oldparent,
          child: [child]
        };
      }
      sorted_by_level["level_" + (level - 1).toString()][
        parentIndex
      ] = newparent;
    });
  }
  sorted_by_level.level_0.sort((a, b) => (a.position > b.position) ? 1 : -1);
  return sorted_by_level.level_0
}

console.log(util.inspect(menu_sorted(mydata.nofilter),false,null,true))

>Solution :

See my fork of your Replit:

https://replit.com/join/wleqdijkot-mattsenne

There are additional places you need to change "child" to "items", including lines 34, 35:

if (oldparent.hasOwnProperty("items") && oldparent.items) {
   var mItems = [...oldparent.items, child];

Also the 2 changes you noted above. Note that we don’t change the local ‘child’ variable, but every reference to ‘items’ being a property needs to accounted for as well.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading