Home > front end >  nesting array of objects based on the key value in js?
nesting array of objects based on the key value in js?

Time:08-01

this question is an extension of the answer provided in this enter image description here

CodePudding user response:

In the middle if your code resets the wrong property:

if (levels.managerName.val !== managerName )
  {
    levels.managerName.val = managerName; 
    levels.managerName.arr = row_1.subRows = [];
    // wrong: levels.managerName.val = ''
    // corrected:
    levels.advisorName.val = '';
    levels.ownerName.arr.push( row_1 );
  }

Side remarks:

  • Make it a habit to separate your statements with a semi-colon. There is automatic semi-colon insertion but you wouldn't be the first to fall into one of its traps. Better take control.
  • It is common practice to reserve names with an initial capital for classes/constructors, so I would use resultData instead of ResultData
  • Related