1

this question is an extension of the answer provided in this question.

I am trying to extend the solution further level to the group by ownerName. But somewhere I made a mistake, couldn't able to get the correct output.

Below I have added my work-around tries.

const data = [{"ownerName":"Apple","mName":"Nikolai","aName":"Dhanush","cName":"Thor Odin","id":"Client 1","gName":"","gAmount":"","gls":2,"value":0.855,"date":"22/1/2022","income":"","rows":[{"ownerName":"","mName":"","aName":"","cName":"","id":"goal-1","gName":"Retirement1","gAmount":10000,"gls":1,"income":"60/40","date":"22/1/2022","value":0.99},{"ownerName":"","mName":"","aName":"","cName":"","id":"goal-2","gName":"Save For Child Education","gAmount":70000,"gls":1,"income":"55/45","date":"5/12/2023","value":0.72}]},{"ownerName":"Apple","mName":"Nikolai","aName":"Dhanush","cName":"Steve Rogers","id":"Client 2","gName":"Save for Investment","gAmount":67000,"gls":1,"value":0.7,"date":"22/1/2022","income":"60/40"},{"ownerName":"Apple","mName":"Nikolai","aName":"Dhanush","cName":"Wanda Vision","id":"Client 3","gls":0,"value":0.9,"date":"","income":""},{"ownerName":"Apple","mName":"Nikolai","aName":"Dhanush","cName":"Tony Stark","id":"Client 4","gName":"","gAmount":"","gls":2,"value":0.29,"date":"27/10/2019","income":"","rows":[{"ownerName":"","mName":"","aName":"","cName":"","id":"goal-4","gName":"Education Loan","gAmount":500,"gls":1,"income":"60/40","date":"27/10/2019","value":0.29},{"ownerName":"","mName":"","aName":"","cName":"","id":"goal-5","gName":"House Loan","gAmount":23000,"gls":1,"income":"30/70","date":"16/6/2022","value":0.29}]},{"ownerName":"Apple","mName":"Nikolai","aName":"Joe","cName":"Hack Eye","id":"Client 5","gName":"Save For World Tour","gAmount":400000,"gls":1,"value":0.74,"date":"","income":"60/40"},{"ownerName":"Apple","mName":"Nikolai","aName":"Joe","cName":"Nick Fury","id":"Client 6","gName":"","gAmount":"","gls":2,"value":0.44499999999999995,"date":"9/3/2022","income":"","rows":[{"ownerName":"","mName":"","aName":"","cName":"","id":"goal-7","gName":"To Build A Workspace","gAmount":42340,"gls":1,"income":"60/40","date":"9/3/2022","value":0.6},{"ownerName":"","mName":"","aName":"","cName":"","id":"goal-8","gName":"Cloud Examination","gAmount":8730,"gls":1,"income":"30/70","date":"9/11/2021","value":0.29}]},{"ownerName":"Apple","mName":"Nikolai","aName":"Joe","cName":"Star Lord","id":"Client 7","gName":"Save For Child Education","gAmount":400000,"gls":1,"value":0.93,"date":"","income":"55/45"},{"ownerName":"Apple","mName":"Rohan","aName":"Pal","cName":"Thanos","id":"Client 8","gName":"","gAmount":"","gls":3,"value":0.29,"date":"2/11/2019","income":"","rows":[{"ownerName":"","mName":"","aName":"","cName":"","id":"goal-10","gName":"Relocation Expense Goal","gAmount":400000,"gls":1,"income":"22/78","date":"2/11/2019","value":0.29},{"ownerName":"","mName":"","aName":"","cName":"","id":"goal-11","gName":"Save for to buy bike","gAmount":400000,"gls":1,"income":"50/50","date":"1/1/2020","value":0.29},{"ownerName":"","mName":"","aName":"","cName":"","id":"goal-12","gName":"Save For Education","gAmount":400000,"gls":1,"income":"65/35","date":"9/5/2022","value":0.29}]},{"ownerName":"Apple","mName":"Rohan","aName":"Pal","cName":"Ego","id":"Client 9","gName":"Save For Education","gAmount":400000,"gls":1,"value":0.72,"date":"","income":"65/35"},{"ownerName":"Apple","mName":"Rohan","aName":"Pal","cName":"Bruce Banner","id":"Client 10","gName":"","gAmount":"","gls":2,"value":0.975,"date":"9/10/2018","income":"","rows":[{"ownerName":"","mName":"","aName":"","cName":"","id":"goal-14","gName":"Car Loan","gAmount":23000,"gls":1,"income":"60/40","date":"9/10/2018","value":0.99},{"mName":"","aName":"","cName":"","id":"goal-15","gName":"Bike Loan","gAmount":4600,"gls":1,"income":"30/70","date":"9/11/2021","value":0.96}]}]

const
nodOrder = 
  { ownerName:'',mName: '',  aName: '', cName: '', id: ''
  , gName: ''   ,  gAmount: '',  gls: '',      value: ''
  , date: '', income: ''
  } 
, levels = 
  { ownerName:   { arr: null, val: '' },
    mName: { arr: null, val: '' }
  , aName: { arr: null, val: '' }
  , cName:  { arr: null }
  }
, ResultData = []
;
data.forEach( ({ ownerName, mName, aName, ...otherProps }) => 
{
let
  row_0 = Object.assign({}, nodOrder, { ownerName })
, row_1 = Object.assign({}, nodOrder, { mName })
, row_2 = Object.assign({}, nodOrder, { aName })
, row_3 = Object.assign({}, nodOrder, otherProps )
  ;

if (levels.ownerName.val !== ownerName )
  {
  levels.ownerName.val = ownerName 
  levels.ownerName.arr = row_0.rows = []
  levels.mName.val = ''
  ResultData.push( row_0 )
  }

if (levels.mName.val !== mName )
  {
    levels.mName.val = mName 
    levels.mName.arr = row_1.rows = []
    levels.mName.val = ''
    levels.ownerName.arr.push( row_1 )
  }

if (levels.aName.val !== aName )
  {
  levels.aName.val = aName 
  levels.aName.arr = row_2.rows = []
  levels.mName.arr.push( row_2 )
  }

levels.cName.arr = (otherProps.rows) ? (row_3.rows = []) : null
levels.aName.arr.push( row_3 )

if (otherProps.rows) 
  {
  otherProps.rows.forEach( subRow => 
    {
    let sRow = Object.assign({}, nodOrder, subRow )
    levels.cName.arr.push( sRow )
    })
  }
})
console.log( JSON.stringify(ResultData ) )

This code is for the data table which shows the data based on the authorization levels. Nesting will change based on the access level.

I am trying to group the data based on the ownerName. Data has been grouped under ownerName but further levels have not been grouped properly. Could you help me by fixing the mistake I am doing?

SDK
  • 1,356
  • 3
  • 19
  • 44
  • Can you post the output you want? – Dohab Jul 31 '22 at 10:38
  • 1
    Meanwhile you shouldn't use `const` here as `levels` and `nodOrder` will be filled later. – Dohab Jul 31 '22 at 11:41
  • @Dohab, I don't agree. `const` is appropriate because the intention is that no further **assignment** will be made to those variables. Mutation is irrelevant to the choice of `let` or `const`. – trincot Jul 31 '22 at 14:20

1 Answers1

1

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
trincot
  • 317,000
  • 35
  • 244
  • 286