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 – https://codesandbox.io/s/tanstack-table-expansion-1t77ks?file=/src/data/table-data.json:0-9490. Which is then used in the @tastack-react-table
. You can see the demo of the application here – https://codesandbox.io/s/tanstack-table-expansion-1t77ks?file=/src/App.js
In the above code conversion, the grouping works properly but only if the values occur in the correct order. That means in the first object you can see the managerName
is Nikolai
the same value is repeated for the next 7
objects. After that, a different managerName
as Rohan
is repeated for the next 3
objects.
Here managerName
as Nikolai
has formed a group, after that managerName
Rohan
has formed another group and again if managerName
Nikolai
repeats means it forms a separate group, instead of not get grouped with already available one.
You can get a better understanding by seeing the below image. You can see the name Nikolai
appears again. It doesn’t form a group with the one already grouped.
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}]}]}]}]
Advertisement
Answer
You need to group by managerName
first, then, for each manager, group their advisors by advisorName
.
- Define a generic helper function that takes an array and a property name to group the items by its value. Using
Array#reduce
, iterate over the array while updating aMap
where the key is the property value (eg manager name) and the value is the list of objects having this value. After that, usingArray#map
andObject#asign
, it would return the grouped pairs as an array of objects each is a template ofrow
with the property (key) and subRows (value). - Having an array of objects like the one you shared, we first need to group them by
managerName
using the above helper. After that, iterating over each manager usingArray#forEach
, we need to group its advisors in thesubRows
using the above helper as well but by theadvisorName
this time.
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; };