Upon suggestion in the original question within Stack Overflow, I'm bringing my question here.
We have created a function that will convert the data into the desired format, but it has a lot of code and is difficult to understand. If a new person has seen this code means, it's very hard for them to know the functionalities.
below is the code for it
const data = [{"id":"Client 1","advisorName":"Dhanush","managerName":"Nikolai","clientName":"Thor Odin","goalName":"","goalAmount":"","goals":2,"score":0.855,"lastModified":"22/1/2022","equityFixedIncome":"","subRows":[{"id":"goal-1","clientName":"","managerName":"","advisorName":"","goalName":"Retirement1","goalAmount":10000,"goals":1,"equityFixedIncome":"60/40","lastModified":"22/1/2022","score":0.99},{"id":"goal-2","clientName":"","managerName":"","advisorName":"","goalName":"Save For Child Education","goalAmount":70000,"goals":1,"equityFixedIncome":"55/45","lastModified":"5/12/2023","score":0.72}]},{"id":"Client 2","advisorName":"Dhanush","managerName":"Nikolai","clientName":"Steve Rogers","goalName":"Save for Investment","goalAmount":67000,"goals":1,"score":0.7,"lastModified":"22/1/2022","equityFixedIncome":"60/40"},{"id":"Client 3","advisorName":"Dhanush","managerName":"Nikolai","clientName":"Wanda Vision","goals":0,"score":0.9,"lastModified":"","equityFixedIncome":""},{"id":"Client 4","advisorName":"Dhanush","managerName":"Nikolai","clientName":"Tony Stark","goalName":"","goalAmount":"","goals":2,"score":0.29,"lastModified":"27/10/2019","equityFixedIncome":"","subRows":[{"id":"goal-4","clientName":"","managerName":"","advisorName":"","goalName":"Education Loan","goalAmount":500,"goals":1,"equityFixedIncome":"60/40","lastModified":"27/10/2019","score":0.29},{"id":"goal-5","clientName":"","managerName":"","advisorName":"","goalName":"House Loan","goalAmount":23000,"goals":1,"equityFixedIncome":"30/70","lastModified":"16/6/2022","score":0.29}]},{"id":"Client 5","advisorName":"Joe","managerName":"Nikolai","clientName":"Hack Eye","goalName":"Save For World Tour","goalAmount":400000,"goals":1,"score":0.74,"lastModified":"","equityFixedIncome":"60/40"},{"id":"Client 6","advisorName":"Joe","managerName":"Nikolai","clientName":"Nick Fury","goalName":"","goalAmount":"","goals":2,"score":0.44499999999999995,"lastModified":"9/3/2022","equityFixedIncome":"","subRows":[{"id":"goal-7","clientName":"","managerName":"","advisorName":"","goalName":"To Build A Workspace","goalAmount":42340,"goals":1,"equityFixedIncome":"60/40","lastModified":"9/3/2022","score":0.6},{"id":"goal-8","clientName":"","managerName":"","advisorName":"","goalName":"Cloud Examination","goalAmount":8730,"goals":1,"equityFixedIncome":"30/70","lastModified":"9/11/2021","score":0.29}]},{"id":"Client 7","advisorName":"Joe","managerName":"Nikolai","clientName":"Star Lord","goalName":"Save For Child Education","goalAmount":400000,"goals":1,"score":0.93,"lastModified":"","equityFixedIncome":"55/45"},{"id":"Client 8","advisorName":"Pal","managerName":"Rohan","clientName":"Thanos","goalName":"","goalAmount":"","goals":3,"score":0.29,"lastModified":"2/11/2019","equityFixedIncome":"","subRows":[{"id":"goal-10","clientName":"","managerName":"","advisorName":"","goalName":"Relocation Expense Goal","goalAmount":400000,"goals":1,"equityFixedIncome":"22/78","lastModified":"2/11/2019","score":0.29},{"id":"goal-11","clientName":"","managerName":"","advisorName":"","goalName":"Save for to buy bike","goalAmount":400000,"goals":1,"equityFixedIncome":"50/50","lastModified":"1/1/2020","score":0.29},{"id":"goal-12","clientName":"","managerName":"","advisorName":"","goalName":"Save For Education","goalAmount":400000,"goals":1,"equityFixedIncome":"65/35","lastModified":"9/5/2022","score":0.29}]},{"id":"Client 9","advisorName":"Pal","managerName":"Rohan","clientName":"Ego","goalName":"Save For Education","goalAmount":400000,"goals":1,"score":0.72,"lastModified":"","equityFixedIncome":"65/35"},{"id":"Client 10","advisorName":"Pal","managerName":"Rohan","clientName":"Bruce Banner","goalName":"","goalAmount":"","goals":2,"score":0.975,"lastModified":"9/10/2018","equityFixedIncome":"","subRows":[{"id":"goal-14","clientName":"","managerName":"","advisorName":"","goalName":"Car Loan","goalAmount":23000,"goals":1,"equityFixedIncome":"60/40","lastModified":"9/10/2018","score":0.99},{"id":"goal-15","clientName":"","managerName":"","advisorName":"","goalName":"Bike Loan","goalAmount":4600,"goals":1,"equityFixedIncome":"30/70","lastModified":"9/11/2021","score":0.96}]}] function firstLevelRestructure(data){ return data.reduce( (acc, row) => { if (row.advisorName !== acc.level1.clientName) { let newRow1 = { advisorName: row.advisorName, managerName: row.managerName, id: "", clientName: "", goalName: "", goalAmount: "", goals: "", score: "", lastModified: "", equityFixedIncome: "", subRows: [], }; acc.result.push(newRow1); acc.level1.clientName = row.advisorName; acc.level1.arr = newRow1.subRows; } let newRow2 = { advisorName: "", managerName: "", id: row.id, clientName: row.clientName, goalName: row.goalName, goalAmount: row.goalAmount, goals: row.goals, score: row.score, lastModified: row.lastModified, equityFixedIncome: row.equityFixedIncome, }; if(row.subRows) { acc.level2.arr = newRow2.subRows = []; } acc.level1.arr.push(newRow2); if (row.subRows) { row.subRows.forEach((subRow) => { acc.level2.arr.push({ ...subRow }); }); } return acc; }, { result: [], level1: { clientName: "", arr: null }, level2: { arr: null }, } ).result; } const restructure = (data, keyName) => { let val = firstLevelRestructure(data) const emptyNode = { managerName: "", advisorName: "", id: "", clientName: "", goalName: "", goalAmount: "", goals: "", score: "", lastModified: "", equityFixedIncome: "", subRows: [], }; const groups = val.reduce((acc, item) => { acc[item[keyName]] ??= []; acc[item[keyName]].push({ ...item, [keyName]: "" }); return acc; }, {}); return Object.entries(groups) .map(([keyValue, subRows]) => ( { ...emptyNode, [keyName]: keyValue , subRows } )); }; console.log(JSON.stringify(restructure(data, 'managerName')));
And why am doing this conversion because, we are creating a multi level nested row expansion table which requires this kind of structure. You can get the working demo link here - https://codesandbox.io/s/tanstack-table-expansion-1t77ks?file=/src/styles.css
And the structured data will be used to create the table with the expansion like in the above image.
Here u can see the formatted data - https://codesandbox.io/s/tanstack-table-expansion-1t77ks?file=/src/data/table-data.json
Instead of calling the function firstLevelRestructure
Is there a way to use the function restructure
to handle it?
I am in need of your help to solve this. Please let me know the feasibility of that.