Hi I am having an array of values like this
Input
[
{
"managerName": "Nikolai",
"advisorName": "Dhanush",
"clientName": "Thor Odin",
"id": "Client 1",
"goalName": "",
"goalAmount": "",
"goals": 2,
"score": 86,
"lastModified": "22/1/2022",
"equityFixedIncome": "",
"subRows": [
{
"managerName": "",
"advisorName": "",
"clientName": "",
"id": "goal-1",
"goalName": "Retirement1",
"goalAmount": 10000,
"goals": "",
"equityFixedIncome": "60/40",
"lastModified": "22/1/2022",
"score": 99
},
{
"managerName": "",
"advisorName": "",
"clientName": "",
"id": "goal-2",
"goalName": "Save For Child Education",
"goalAmount": 70000,
"goals": "",
"equityFixedIncome": "55/45",
"lastModified": "5/12/2023",
"score": 72
}
]
},
{
"managerName": "Nikolai",
"advisorName": "Dhanush",
"clientName": "Steve Rogers",
"id": "Client 2",
"goalName": "Save for Investment",
"goalAmount": 67000,
"goals": 1,
"score": 70,
"lastModified": "22/1/2022",
"equityFixedIncome": "60/40"
},
{
"managerName": "Nikolai",
"advisorName": "Dhanush",
"clientName": "Wanda Vision",
"id": "Client 3",
"goals": 0,
"score": 0,
"lastModified": "",
"equityFixedIncome": ""
},
{
"managerName": "Nikolai",
"advisorName": "Dhanush",
"clientName": "Tony Stark",
"id": "Client 4",
"goalName": "",
"goalAmount": "",
"goals": 2,
"score": 29,
"lastModified": "27/10/2019",
"equityFixedIncome": "",
"subRows": [
{
"managerName": "",
"advisorName": "",
"clientName": "",
"id": "goal-4",
"goalName": "Education Loan",
"goalAmount": 500,
"goals": "",
"equityFixedIncome": "60/40",
"lastModified": "27/10/2019",
"score": 29
},
{
"managerName": "",
"advisorName": "",
"clientName": "",
"id": "goal-5",
"goalName": "House Loan",
"goalAmount": 23000,
"goals": "",
"equityFixedIncome": "30/70",
"lastModified": "16/6/2022",
"score": 29
}
]
},
{
"managerName": "Nikolai",
"advisorName": "Joe",
"clientName": "Hack Eye",
"id": "Client 5",
"goalName": "Save For World Tour",
"goalAmount": 400000,
"goals": 1,
"score": 74,
"lastModified": "",
"equityFixedIncome": "60/40"
},
{
"managerName": "Nikolai",
"advisorName": "Joe",
"clientName": "Nick Fury",
"id": "Client 6",
"goalName": "",
"goalAmount": "",
"goals": 2,
"score": 44,
"lastModified": "9/3/2022",
"equityFixedIncome": "",
"subRows": [
{
"managerName": "",
"advisorName": "",
"clientName": "",
"id": "goal-7",
"goalName": "To Build A Workspace",
"goalAmount": 42340,
"goals": "",
"equityFixedIncome": "60/40",
"lastModified": "9/3/2022",
"score": 60
},
{
"managerName": "",
"advisorName": "",
"clientName": "",
"id": "goal-8",
"goalName": "Cloud Examination",
"goalAmount": 8730,
"goals": "",
"equityFixedIncome": "30/70",
"lastModified": "9/11/2021",
"score": 29
}
]
},
{
"managerName": "Nikolai",
"advisorName": "Joe",
"clientName": "Star Lord",
"id": "Client 7",
"goalName": "Save For Child Education",
"goalAmount": 400000,
"goals": 1,
"score": 93,
"lastModified": "",
"equityFixedIncome": "55/45"
},
{
"managerName": "Rohan",
"advisorName": "Pal",
"clientName": "Thanos",
"id": "Client 8",
"goalName": "",
"goalAmount": "",
"goals": 3,
"score": 29,
"lastModified": "2/11/2019",
"equityFixedIncome": "",
"subRows": [
{
"managerName": "",
"advisorName": "",
"clientName": "",
"id": "goal-10",
"goalName": "Relocation Expense Goal",
"goalAmount": 400000,
"goals": "",
"equityFixedIncome": "22/78",
"lastModified": "2/11/2019",
"score": 29
},
{
"managerName": "",
"advisorName": "",
"clientName": "",
"id": "goal-11",
"goalName": "Save for to buy bike",
"goalAmount": 400000,
"goals": "",
"equityFixedIncome": "50/50",
"lastModified": "1/1/2020",
"score": 29
},
{
"managerName": "",
"advisorName": "",
"clientName": "",
"id": "goal-12",
"goalName": "Save For Education",
"goalAmount": 400000,
"goals": "",
"equityFixedIncome": "65/35",
"lastModified": "9/5/2022",
"score": 29
}
]
},
{
"managerName": "Rohan",
"advisorName": "Pal",
"clientName": "Ego",
"id": "Client 9",
"goalName": "Save For Education",
"goalAmount": 400000,
"goals": 1,
"score": 72,
"lastModified": "",
"equityFixedIncome": "65/35"
},
{
"managerName": "Rohan",
"advisorName": "Pal",
"clientName": "Bruce Banner",
"id": "Client 10",
"goalName": "",
"goalAmount": "",
"goals": 2,
"score": 98,
"lastModified": "9/10/2018",
"equityFixedIncome": "",
"subRows": [
{
"managerName": "",
"advisorName": "",
"clientName": "",
"id": "goal-14",
"goalName": "Car Loan",
"goalAmount": 23000,
"goals": "",
"equityFixedIncome": "60/40",
"lastModified": "9/10/2018",
"score": 99
},
{
"managerName": "",
"advisorName": "",
"clientName": "",
"id": "goal-15",
"goalName": "Bike Loan",
"goalAmount": 4600,
"goals": "",
"equityFixedIncome": "30/70",
"lastModified": "9/11/2021",
"score": 96
}
]
},
{
"managerName": "Rohan",
"advisorName": "Dhanush",
"clientName": "Bruce Banner",
"id": "Client 11",
"goalName": "",
"goalAmount": "",
"goals": 2,
"score": 98,
"lastModified": "9/10/2018",
"equityFixedIncome": "",
"subRows": [
{
"managerName": "",
"advisorName": "",
"clientName": "",
"id": "goal-14",
"goalName": "Car Loan",
"goalAmount": 23000,
"goals": "",
"equityFixedIncome": "60/40",
"lastModified": "9/10/2018",
"score": 99
},
{
"managerName": "",
"advisorName": "",
"clientName": "",
"id": "goal-15",
"goalName": "Bike Loan",
"goalAmount": 4600,
"goals": "",
"equityFixedIncome": "30/70",
"lastModified": "9/11/2021",
"score": 96
}
]
},
{
"managerName": "Nikolai",
"advisorName": "Dhanush",
"clientName": "Bruce Banner",
"id": "Client 12",
"goalName": "",
"goalAmount": "",
"goals": 2,
"score": 98,
"lastModified": "9/10/2018",
"equityFixedIncome": "",
"subRows": [
{
"managerName": "",
"advisorName": "",
"clientName": "",
"id": "goal-14",
"goalName": "Car Loan",
"goalAmount": 23000,
"goals": "",
"equityFixedIncome": "60/40",
"lastModified": "9/10/2018",
"score": 99
},
{
"managerName": "",
"advisorName": "",
"clientName": "",
"id": "goal-15",
"goalName": "Bike Loan",
"goalAmount": 4600,
"goals": "",
"equityFixedIncome": "30/70",
"lastModified": "9/11/2021",
"score": 96
}
]
},
{
"managerName": "Suresh Murugaiyan",
"advisorName": "Loki",
"clientName": "Yondu",
"id": "Client 13",
"goalName": "",
"goalAmount": "",
"goals": 2,
"score": 98,
"lastModified": "9/10/2018",
"equityFixedIncome": "",
"subRows": [
{
"managerName": "",
"advisorName": "",
"clientName": "",
"id": "goal-14",
"goalName": "Car Loan",
"goalAmount": 23000,
"goals": "",
"equityFixedIncome": "60/40",
"lastModified": "9/10/2018",
"score": 99
},
{
"managerName": "",
"advisorName": "",
"clientName": "",
"id": "goal-15",
"goalName": "Bike Loan",
"goalAmount": 4600,
"goals": "",
"equityFixedIncome": "30/70",
"lastModified": "9/11/2021",
"score": 96
}
]
},
{
"managerName": "Nikolai",
"advisorName": "Dhanush",
"clientName": "Thor Odin",
"id": "Client 1",
"goalName": "",
"goalAmount": "",
"goals": 2,
"score": 86,
"lastModified": "22/1/2022",
"equityFixedIncome": "",
"subRows": [
{
"managerName": "",
"advisorName": "",
"clientName": "",
"id": "goal-1",
"goalName": "Retirement1",
"goalAmount": 10000,
"goals": "",
"equityFixedIncome": "60/40",
"lastModified": "22/1/2022",
"score": 99
},
{
"managerName": "",
"advisorName": "",
"clientName": "",
"id": "goal-2",
"goalName": "Save For Child Education",
"goalAmount": 70000,
"goals": "",
"equityFixedIncome": "55/45",
"lastModified": "5/12/2023",
"score": 72
}
]
}
]
----------
By using the below function
const data = [{"managerName":"Nikolai","advisorName":"Dhanush","clientName":"Thor Odin","id":"Client 1","goalName":"","goalAmount":"","goals":2,"score":86,"lastModified":"22/1/2022","equityFixedIncome":"","subRows":[{"managerName":"","advisorName":"","clientName":"","id":"goal-1","goalName":"Retirement1","goalAmount":10000,"goals":"","equityFixedIncome":"60/40","lastModified":"22/1/2022","score":99},{"managerName":"","advisorName":"","clientName":"","id":"goal-2","goalName":"Save For Child Education","goalAmount":70000,"goals":"","equityFixedIncome":"55/45","lastModified":"5/12/2023","score":72}]},{"managerName":"Nikolai","advisorName":"Dhanush","clientName":"Steve Rogers","id":"Client 2","goalName":"Save for Investment","goalAmount":67000,"goals":1,"score":70,"lastModified":"22/1/2022","equityFixedIncome":"60/40"},{"managerName":"Nikolai","advisorName":"Dhanush","clientName":"Wanda Vision","id":"Client 3","goals":0,"score":0,"lastModified":"","equityFixedIncome":""},{"managerName":"Nikolai","advisorName":"Dhanush","clientName":"Tony Stark","id":"Client 4","goalName":"","goalAmount":"","goals":2,"score":29,"lastModified":"27/10/2019","equityFixedIncome":"","subRows":[{"managerName":"","advisorName":"","clientName":"","id":"goal-4","goalName":"Education Loan","goalAmount":500,"goals":"","equityFixedIncome":"60/40","lastModified":"27/10/2019","score":29},{"managerName":"","advisorName":"","clientName":"","id":"goal-5","goalName":"House Loan","goalAmount":23000,"goals":"","equityFixedIncome":"30/70","lastModified":"16/6/2022","score":29}]},{"managerName":"Nikolai","advisorName":"Joe","clientName":"Hack Eye","id":"Client 5","goalName":"Save For World Tour","goalAmount":400000,"goals":1,"score":74,"lastModified":"","equityFixedIncome":"60/40"},{"managerName":"Nikolai","advisorName":"Joe","clientName":"Nick Fury","id":"Client 6","goalName":"","goalAmount":"","goals":2,"score":44,"lastModified":"9/3/2022","equityFixedIncome":"","subRows":[{"managerName":"","advisorName":"","clientName":"","id":"goal-7","goalName":"To Build A Workspace","goalAmount":42340,"goals":"","equityFixedIncome":"60/40","lastModified":"9/3/2022","score":60},{"managerName":"","advisorName":"","clientName":"","id":"goal-8","goalName":"Cloud Examination","goalAmount":8730,"goals":"","equityFixedIncome":"30/70","lastModified":"9/11/2021","score":29}]},{"managerName":"Nikolai","advisorName":"Joe","clientName":"Star Lord","id":"Client 7","goalName":"Save For Child Education","goalAmount":400000,"goals":1,"score":93,"lastModified":"","equityFixedIncome":"55/45"},{"managerName":"Rohan","advisorName":"Pal","clientName":"Thanos","id":"Client 8","goalName":"","goalAmount":"","goals":3,"score":29,"lastModified":"2/11/2019","equityFixedIncome":"","subRows":[{"managerName":"","advisorName":"","clientName":"","id":"goal-10","goalName":"Relocation Expense Goal","goalAmount":400000,"goals":"","equityFixedIncome":"22/78","lastModified":"2/11/2019","score":29},{"managerName":"","advisorName":"","clientName":"","id":"goal-11","goalName":"Save for to buy bike","goalAmount":400000,"goals":"","equityFixedIncome":"50/50","lastModified":"1/1/2020","score":29},{"managerName":"","advisorName":"","clientName":"","id":"goal-12","goalName":"Save For Education","goalAmount":400000,"goals":"","equityFixedIncome":"65/35","lastModified":"9/5/2022","score":29}]},{"managerName":"Rohan","advisorName":"Pal","clientName":"Ego","id":"Client 9","goalName":"Save For Education","goalAmount":400000,"goals":1,"score":72,"lastModified":"","equityFixedIncome":"65/35"},{"managerName":"Rohan","advisorName":"Pal","clientName":"Bruce Banner","id":"Client 10","goalName":"","goalAmount":"","goals":2,"score":98,"lastModified":"9/10/2018","equityFixedIncome":"","subRows":[{"managerName":"","advisorName":"","clientName":"","id":"goal-14","goalName":"Car Loan","goalAmount":23000,"goals":"","equityFixedIncome":"60/40","lastModified":"9/10/2018","score":99},{"managerName":"","advisorName":"","clientName":"","id":"goal-15","goalName":"Bike Loan","goalAmount":4600,"goals":"","equityFixedIncome":"30/70","lastModified":"9/11/2021","score":96}]},{"managerName":"Rohan","advisorName":"Dhanush","clientName":"Bruce Banner","id":"Client 11","goalName":"","goalAmount":"","goals":2,"score":98,"lastModified":"9/10/2018","equityFixedIncome":"","subRows":[{"managerName":"","advisorName":"","clientName":"","id":"goal-14","goalName":"Car Loan","goalAmount":23000,"goals":"","equityFixedIncome":"60/40","lastModified":"9/10/2018","score":99},{"managerName":"","advisorName":"","clientName":"","id":"goal-15","goalName":"Bike Loan","goalAmount":4600,"goals":"","equityFixedIncome":"30/70","lastModified":"9/11/2021","score":96}]},{"managerName":"Nikolai","advisorName":"Dhanush","clientName":"Bruce Banner","id":"Client 12","goalName":"","goalAmount":"","goals":2,"score":98,"lastModified":"9/10/2018","equityFixedIncome":"","subRows":[{"managerName":"","advisorName":"","clientName":"","id":"goal-14","goalName":"Car Loan","goalAmount":23000,"goals":"","equityFixedIncome":"60/40","lastModified":"9/10/2018","score":99},{"managerName":"","advisorName":"","clientName":"","id":"goal-15","goalName":"Bike Loan","goalAmount":4600,"goals":"","equityFixedIncome":"30/70","lastModified":"9/11/2021","score":96}]},{"managerName":"Suresh Murugaiyan","advisorName":"Loki","clientName":"Yondu","id":"Client 13","goalName":"","goalAmount":"","goals":2,"score":98,"lastModified":"9/10/2018","equityFixedIncome":"","subRows":[{"managerName":"","advisorName":"","clientName":"","id":"goal-14","goalName":"Car Loan","goalAmount":23000,"goals":"","equityFixedIncome":"60/40","lastModified":"9/10/2018","score":99},{"managerName":"","advisorName":"","clientName":"","id":"goal-15","goalName":"Bike Loan","goalAmount":4600,"goals":"","equityFixedIncome":"30/70","lastModified":"9/11/2021","score":96}]},{"managerName":"Nikolai","advisorName":"Dhanush","clientName":"Thor Odin","id":"Client 1","goalName":"","goalAmount":"","goals":2,"score":86,"lastModified":"22/1/2022","equityFixedIncome":"","subRows":[{"managerName":"","advisorName":"","clientName":"","id":"goal-1","goalName":"Retirement1","goalAmount":10000,"goals":"","equityFixedIncome":"60/40","lastModified":"22/1/2022","score":99},{"managerName":"","advisorName":"","clientName":"","id":"goal-2","goalName":"Save For Child Education","goalAmount":70000,"goals":"","equityFixedIncome":"55/45","lastModified":"5/12/2023","score":72}]}]
function groupByManager(val){
const
nodOrder =
{ managerName: '', advisorName: '', clientName: '', id: ''
, goalName: '' , goalAmount: '', goals: '', score: ''
, lastModified: '', equityFixedIncome: ''
}
, levels =
{ managerName: { arr: null, val: '' }
, advisorName: { arr: null, val: '' }
, clientName: { arr: null }
}
, resultData = []
;
val.forEach( ({ managerName, advisorName, ...otherProps }) =>
{
let
row_0 = Object.assign({}, nodOrder, { managerName })
, row_1 = Object.assign({}, nodOrder, { advisorName })
, row_2 = Object.assign({}, nodOrder, otherProps )
;
if (levels.managerName.val !== managerName )
{
levels.managerName.val = managerName
levels.managerName.arr = row_0.subRows = []
levels.advisorName.val = ''
resultData.push( row_0 )
}
if (levels.advisorName.val !== advisorName )
{
levels.advisorName.val = advisorName
levels.advisorName.arr = row_1.subRows = []
levels.managerName.arr.push( row_1 )
}
levels.clientName.arr = (otherProps.subRows) ? (row_2.subRows = []) : null
levels.advisorName.arr.push( row_2 )
if (otherProps.subRows)
{
otherProps.subRows.forEach( subRow =>
{
let sRow = Object.assign({}, nodOrder, subRow )
levels.clientName.arr.push( sRow )
})
}
})
return resultData
}
console.log(JSON.stringify(groupByManager(data)))
I am converting the data into the format like this -
Also the advisorName
also not grouped properly. It does the same error.
I tried to solve this, but I don't know where I am making mistake, please help me to resolve this one.
Expected Output
[{"managerName":"Nikolai","advisorName":"","id":"","clientName":"","goalName":"","goalAmount":"","goals":"","score":"","lastModified":"","equityFixedIncome":"","subRows":[{"advisorName":"Dhanush","managerName":"","id":"","clientName":"","goalName":"","goalAmount":"","goals":"","score":"","lastModified":"","equityFixedIncome":"","subRows":[{"advisorName":"","managerName":"","id":"Client 1","clientName":"Thor Odin","goalName":"","goalAmount":"","goals":2,"score":86,"lastModified":"22/1/2022","equityFixedIncome":"","subRows":[{"managerName":"","advisorName":"","clientName":"","id":"goal-1","goalName":"Retirement1","goalAmount":10000,"goals":"","equityFixedIncome":"60/40","lastModified":"22/1/2022","score":99},{"managerName":"","advisorName":"","clientName":"","id":"goal-2","goalName":"Save For Child Education","goalAmount":70000,"goals":"","equityFixedIncome":"55/45","lastModified":"5/12/2023","score":72}]},{"advisorName":"","managerName":"","id":"Client 2","clientName":"Steve Rogers","goalName":"Save for Investment","goalAmount":67000,"goals":1,"score":70,"lastModified":"22/1/2022","equityFixedIncome":"60/40"},{"advisorName":"","managerName":"","id":"Client 3","clientName":"Wanda Vision","goals":0,"score":0,"lastModified":"","equityFixedIncome":""},{"advisorName":"","managerName":"","id":"Client 4","clientName":"Tony Stark","goalName":"","goalAmount":"","goals":2,"score":29,"lastModified":"27/10/2019","equityFixedIncome":"","subRows":[{"managerName":"","advisorName":"","clientName":"","id":"goal-4","goalName":"Education Loan","goalAmount":500,"goals":"","equityFixedIncome":"60/40","lastModified":"27/10/2019","score":29},{"managerName":"","advisorName":"","clientName":"","id":"goal-5","goalName":"House Loan","goalAmount":23000,"goals":"","equityFixedIncome":"30/70","lastModified":"16/6/2022","score":29}]},{"advisorName":"","managerName":"","id":"Client 11","clientName":"Bruce Banner","goalName":"","goalAmount":"","goals":2,"score":98,"lastModified":"9/10/2018","equityFixedIncome":"","subRows":[{"managerName":"","advisorName":"","clientName":"","id":"goal-14","goalName":"Car Loan","goalAmount":23000,"goals":"","equityFixedIncome":"60/40","lastModified":"9/10/2018","score":99},{"managerName":"","advisorName":"","clientName":"","id":"goal-15","goalName":"Bike Loan","goalAmount":4600,"goals":"","equityFixedIncome":"30/70","lastModified":"9/11/2021","score":96}]},{"advisorName":"","managerName":"","id":"Client 12","clientName":"Bruce Banner","goalName":"","goalAmount":"","goals":2,"score":98,"lastModified":"9/10/2018","equityFixedIncome":"","subRows":[{"managerName":"","advisorName":"","clientName":"","id":"goal-14","goalName":"Car Loan","goalAmount":23000,"goals":"","equityFixedIncome":"60/40","lastModified":"9/10/2018","score":99},{"managerName":"","advisorName":"","clientName":"","id":"goal-15","goalName":"Bike Loan","goalAmount":4600,"goals":"","equityFixedIncome":"30/70","lastModified":"9/11/2021","score":96}]},{"advisorName":"","managerName":"","id":"Client 1","clientName":"Thor Odin","goalName":"","goalAmount":"","goals":2,"score":86,"lastModified":"22/1/2022","equityFixedIncome":"","subRows":[{"managerName":"","advisorName":"","clientName":"","id":"goal-1","goalName":"Retirement1","goalAmount":10000,"goals":"","equityFixedIncome":"60/40","lastModified":"22/1/2022","score":99},{"managerName":"","advisorName":"","clientName":"","id":"goal-2","goalName":"Save For Child Education","goalAmount":70000,"goals":"","equityFixedIncome":"55/45","lastModified":"5/12/2023","score":72}]}]},{"advisorName":"Joe","managerName":"","id":"","clientName":"","goalName":"","goalAmount":"","goals":"","score":"","lastModified":"","equityFixedIncome":"","subRows":[{"advisorName":"","managerName":"","id":"Client 5","clientName":"Hack Eye","goalName":"Save For World Tour","goalAmount":400000,"goals":1,"score":74,"lastModified":"","equityFixedIncome":"60/40"},{"advisorName":"","managerName":"","id":"Client 6","clientName":"Nick Fury","goalName":"","goalAmount":"","goals":2,"score":44,"lastModified":"9/3/2022","equityFixedIncome":"","subRows":[{"managerName":"","advisorName":"","clientName":"","id":"goal-7","goalName":"To Build A Workspace","goalAmount":42340,"goals":"","equityFixedIncome":"60/40","lastModified":"9/3/2022","score":60},{"managerName":"","advisorName":"","clientName":"","id":"goal-8","goalName":"Cloud Examination","goalAmount":8730,"goals":"","equityFixedIncome":"30/70","lastModified":"9/11/2021","score":29}]},{"advisorName":"","managerName":"","id":"Client 7","clientName":"Star Lord","goalName":"Save For Child Education","goalAmount":400000,"goals":1,"score":93,"lastModified":"","equityFixedIncome":"55/45"}]}]},{"managerName":"Suresh Murugaiyan","advisorName":"","id":"","clientName":"","goalName":"","goalAmount":"","goals":"","score":"","lastModified":"","equityFixedIncome":"","subRows":[{"advisorName":"Loki","managerName":"","id":"","clientName":"","goalName":"","goalAmount":"","goals":"","score":"","lastModified":"","equityFixedIncome":"","subRows":[{"advisorName":"","managerName":"","id":"Client 13","clientName":"Yondu","goalName":"","goalAmount":"","goals":2,"score":98,"lastModified":"9/10/2018","equityFixedIncome":"","subRows":[{"managerName":"","advisorName":"","clientName":"","id":"goal-14","goalName":"Car Loan","goalAmount":23000,"goals":"","equityFixedIncome":"60/40","lastModified":"9/10/2018","score":99},{"managerName":"","advisorName":"","clientName":"","id":"goal-15","goalName":"Bike Loan","goalAmount":4600,"goals":"","equityFixedIncome":"30/70","lastModified":"9/11/2021","score":96}]}]}]},{"managerName":"Rohan","advisorName":"","id":"","clientName":"","goalName":"","goalAmount":"","goals":"","score":"","lastModified":"","equityFixedIncome":"","subRows":[{"advisorName":"Pal","managerName":"","id":"","clientName":"","goalName":"","goalAmount":"","goals":"","score":"","lastModified":"","equityFixedIncome":"","subRows":[{"advisorName":"","managerName":"","id":"Client 8","clientName":"Thanos","goalName":"","goalAmount":"","goals":3,"score":29,"lastModified":"2/11/2019","equityFixedIncome":"","subRows":[{"managerName":"","advisorName":"","clientName":"","id":"goal-10","goalName":"Relocation Expense Goal","goalAmount":400000,"goals":"","equityFixedIncome":"22/78","lastModified":"2/11/2019","score":29},{"managerName":"","advisorName":"","clientName":"","id":"goal-11","goalName":"Save for to buy bike","goalAmount":400000,"goals":"","equityFixedIncome":"50/50","lastModified":"1/1/2020","score":29},{"managerName":"","advisorName":"","clientName":"","id":"goal-12","goalName":"Save For Education","goalAmount":400000,"goals":"","equityFixedIncome":"65/35","lastModified":"9/5/2022","score":29}]},{"advisorName":"","managerName":"","id":"Client 9","clientName":"Ego","goalName":"Save For Education","goalAmount":400000,"goals":1,"score":72,"lastModified":"","equityFixedIncome":"65/35"},{"advisorName":"","managerName":"","id":"Client 10","clientName":"Bruce Banner","goalName":"","goalAmount":"","goals":2,"score":98,"lastModified":"9/10/2018","equityFixedIncome":"","subRows":[{"managerName":"","advisorName":"","clientName":"","id":"goal-14","goalName":"Car Loan","goalAmount":23000,"goals":"","equityFixedIncome":"60/40","lastModified":"9/10/2018","score":99},{"managerName":"","advisorName":"","clientName":"","id":"goal-15","goalName":"Bike Loan","goalAmount":4600,"goals":"","equityFixedIncome":"30/70","lastModified":"9/11/2021","score":96}]}]}]}]
CodePudding user response:
You need to group by managerName
first, then, for each manager, group their advisors by advisorName
:
const _groupBy = (arr = [], prop) => {
const row = {
managerName: "",
advisorName: "",
clientName: "",
id: "",
goalName: "",
goalAmount: "",
goals: "",
score: "",
lastModified: "",
equityFixedIncome: ""
};
const map = arr.reduce(
(map, { [prop]: propToGroupBy, ...props }) =>
map.set(propToGroupBy, [
...(map.get(propToGroupBy) ?? []),
{ [prop]: "", ...props }
]),
new Map()
);
return [...map.entries()].map(([propToGroupBy, subRows]) =>
Object.assign({}, row, {
[prop]: propToGroupBy,
subRows
})
);
};
const groupData = (arr = []) => {
const managerList = _groupBy(arr, "managerName");
managerList.forEach((manager) => {
manager.subRows = _groupBy(manager.subRows, "advisorName");
});
return managerList;
};