Skip to main content
Notice removed Content dispute by CommunityBot
Post Unlocked by CommunityBot
Post Locked by Mast
Notice added Content dispute by Mast
Rollback to Revision 8
Source Link
pacmaninbw
  • 26.2k
  • 13
  • 47
  • 114
const data = [{"id":"Client 1","aName""advisorName":"Dhanush","mName""managerName":"Nikolai","cName""clientName":"Thor Odin","gName""goalName":"","gAmount""goalAmount":"","gls""goals":2,"value""score":0.855,"date""lastModified":"22/1/2022","income""equityFixedIncome":"","rows""subRows":[{"id":"goal-1","cName""clientName":"","mName""managerName":"","aName""advisorName":"","gName""goalName":"Retirement1","gAmount""goalAmount":10000,"gls""goals":1,"income""equityFixedIncome":"60/40","date""lastModified":"22/1/2022","value""score":0.99},{"id":"goal-2","cName""clientName":"","mName""managerName":"","aName""advisorName":"","gName""goalName":"Save For Child Education","gAmount""goalAmount":70000,"gls""goals":1,"income""equityFixedIncome":"55/45","date""lastModified":"5/12/2023","value""score":0.72}]},{"id":"Client 2","aName""advisorName":"Dhanush","mName""managerName":"Nikolai","cName""clientName":"Steve Rogers","gName""goalName":"Save for Investment","gAmount""goalAmount":67000,"gls""goals":1,"value""score":0.7,"date""lastModified":"22/1/2022","income""equityFixedIncome":"60/40"},{"id":"Client 3","aName""advisorName":"Dhanush","mName""managerName":"Nikolai","cName""clientName":"Wanda Vision","gls""goals":0,"value""score":0.9,"date""lastModified":"","income""equityFixedIncome":""},{"id":"Client 4","aName""advisorName":"Dhanush","mName""managerName":"Nikolai","cName""clientName":"Tony Stark","gName""goalName":"","gAmount""goalAmount":"","gls""goals":2,"value""score":0.29,"date""lastModified":"27/10/2019","income""equityFixedIncome":"","rows""subRows":[{"id":"goal-4","cName""clientName":"","mName""managerName":"","aName""advisorName":"","gName""goalName":"Education Loan","gAmount""goalAmount":500,"gls""goals":1,"income""equityFixedIncome":"60/40","date""lastModified":"27/10/2019","value""score":0.29},{"id":"goal-5","cName""clientName":"","mName""managerName":"","aName""advisorName":"","gName""goalName":"House Loan","gAmount""goalAmount":23000,"gls""goals":1,"income""equityFixedIncome":"30/70","date""lastModified":"16/6/2022","value""score":0.29}]},{"id":"Client 5","aName""advisorName":"Joe","mName""managerName":"Nikolai","cName""clientName":"Hack Eye","gName""goalName":"Save For World Tour","gAmount""goalAmount":400000,"gls""goals":1,"value""score":0.74,"date""lastModified":"","income""equityFixedIncome":"60/40"},{"id":"Client 6","aName""advisorName":"Joe","mName""managerName":"Nikolai","cName""clientName":"Nick Fury","gName""goalName":"","gAmount""goalAmount":"","gls""goals":2,"value""score":0.44499999999999995,"date""lastModified":"9/3/2022","income""equityFixedIncome":"","rows""subRows":[{"id":"goal-7","cName""clientName":"","mName""managerName":"","aName""advisorName":"","gName""goalName":"To Build A Workspace","gAmount""goalAmount":42340,"gls""goals":1,"income""equityFixedIncome":"60/40","date""lastModified":"9/3/2022","value""score":0.6},{"id":"goal-8","cName""clientName":"","mName""managerName":"","aName""advisorName":"","gName""goalName":"Cloud Examination","gAmount""goalAmount":8730,"gls""goals":1,"income""equityFixedIncome":"30/70","date""lastModified":"9/11/2021","value""score":0.29}]},{"id":"Client 7","aName""advisorName":"Joe","mName""managerName":"Nikolai","cName""clientName":"Star Lord","gName""goalName":"Save For Child Education","gAmount""goalAmount":400000,"gls""goals":1,"value""score":0.93,"date""lastModified":"","income""equityFixedIncome":"55/45"},{"id":"Client 8","aName""advisorName":"Pal","mName""managerName":"Rohan","cName""clientName":"Thanos","gName""goalName":"","gAmount""goalAmount":"","gls""goals":3,"value""score":0.29,"date""lastModified":"2/11/2019","income""equityFixedIncome":"","rows""subRows":[{"id":"goal-10","cName""clientName":"","mName""managerName":"","aName""advisorName":"","gName""goalName":"Relocation Expense Goal","gAmount""goalAmount":400000,"gls""goals":1,"income""equityFixedIncome":"22/78","date""lastModified":"2/11/2019","value""score":0.29},{"id":"goal-11","cName""clientName":"","mName""managerName":"","aName""advisorName":"","gName""goalName":"Save for to buy bike","gAmount""goalAmount":400000,"gls""goals":1,"income""equityFixedIncome":"50/50","date""lastModified":"1/1/2020","value""score":0.29},{"id":"goal-12","cName""clientName":"","mName""managerName":"","aName""advisorName":"","gName""goalName":"Save For Education","gAmount""goalAmount":400000,"gls""goals":1,"income""equityFixedIncome":"65/35","date""lastModified":"9/5/2022","value""score":0.29}]},{"id":"Client 9","aName""advisorName":"Pal","mName""managerName":"Rohan","cName""clientName":"Ego","gName""goalName":"Save For Education","gAmount""goalAmount":400000,"gls""goals":1,"value""score":0.72,"date""lastModified":"","income""equityFixedIncome":"65/35"},{"id":"Client 10","aName""advisorName":"Pal","mName""managerName":"Rohan","cName""clientName":"Bruce Banner","gName""goalName":"","gAmount""goalAmount":"","gls""goals":2,"value""score":0.975,"date""lastModified":"9/10/2018","income""equityFixedIncome":"","rows""subRows":[{"id":"goal-14","cName""clientName":"","mName""managerName":"","aName""advisorName":"","gName""goalName":"Car Loan","gAmount""goalAmount":23000,"gls""goals":1,"income""equityFixedIncome":"60/40","date""lastModified":"9/10/2018","value""score":0.99},{"id":"goal-15","cName""clientName":"","mName""managerName":"","aName""advisorName":"","gName""goalName":"Bike Loan","gAmount""goalAmount":4600,"gls""goals":1,"income""equityFixedIncome":"30/70","date""lastModified":"9/11/2021","value""score":0.96}]}]

function firstLevelRestructure(data){
  return data.reduce(
    (acc, row) => {
      if (row.aNameadvisorName !== acc.level1.cNameclientName) {
        let newRow1 = {
          aNameadvisorName: row.aNameadvisorName,
          mNamemanagerName: row.mNamemanagerName,
          id: "",
          cNameclientName: "",
          gNamegoalName: "",
          gAmountgoalAmount: "",
          glsgoals: "",
          valuescore: "",
          datelastModified: "",
          incomeequityFixedIncome: "",
          rowssubRows: [],
        };
        acc.result.push(newRow1);
        acc.level1.cNameclientName = row.aName;advisorName;
        acc.level1.arr = newRow1.rows;subRows;
      }
      let newRow2 = {
        aNameadvisorName: "",
        mNamemanagerName: "",
        id: row.id,
        cNameclientName: row.cNameclientName,
        gNamegoalName: row.gNamegoalName,
        gAmountgoalAmount: row.gAmountgoalAmount,
        glsgoals: row.glsgoals,
        valuescore: row.valuescore,
        datelastModified: row.datelastModified,
        incomeequityFixedIncome: row.incomeequityFixedIncome,
      };
  
      if(row.rowssubRows) {
        acc.level2.arr = newRow2.rowssubRows = [];
      }
      acc.level1.arr.push(newRow2);
  
      if (row.rowssubRows) {
        row.rowssubRows.forEach((subRow) => {
          acc.level2.arr.push({ ...subRow });
        });
      }
      return acc;
    },
    {
      result: [],
      level1: { cNameclientName: "", arr: null },
      level2: { arr: null },
    }
  ).result;
}



const restructure = (data, keyName) => {
  let val = firstLevelRestructure(data)
  const emptyNode = {
    mNamemanagerName: "",
    aNameadvisorName: "",
    id: "",
    cNameclientName: "",
    gNamegoalName: "",
    gAmountgoalAmount: "",
    glsgoals: "",
    valuescore: "",
    datelastModified: "",
    incomeequityFixedIncome: "",
    rowssubRows: [],
  };
  
  const groups = val.reduce((acc, item) => {
    acc[item[keyName]] ??= [];
    acc[item[keyName]].push({ ...item, [keyName]: "" });
    return acc;
  }, {});

  return Object.entries(groups)
    .map(([keyValue, rows]subRows]) => (
        { ...emptyNode,  [keyName]: keyValue , rowssubRows }
    ));  
};

console.log(JSON.stringify(restructure(data, 'mName''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

enter image description here

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

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

function firstLevelRestructure(data){
  return data.reduce(
    (acc, row) => {
      if (row.aName !== acc.level1.cName) {
        let newRow1 = {
          aName: row.aName,
          mName: row.mName,
          id: "",
          cName: "",
          gName: "",
          gAmount: "",
          gls: "",
          value: "",
          date: "",
          income: "",
          rows: [],
        };
        acc.result.push(newRow1);
        acc.level1.cName = row.aName;
        acc.level1.arr = newRow1.rows;
      }
      let newRow2 = {
        aName: "",
        mName: "",
        id: row.id,
        cName: row.cName,
        gName: row.gName,
        gAmount: row.gAmount,
        gls: row.gls,
        value: row.value,
        date: row.date,
        income: row.income,
      };
  
      if(row.rows) {
        acc.level2.arr = newRow2.rows = [];
      }
      acc.level1.arr.push(newRow2);
  
      if (row.rows) {
        row.rows.forEach((subRow) => {
          acc.level2.arr.push({ ...subRow });
        });
      }
      return acc;
    },
    {
      result: [],
      level1: { cName: "", arr: null },
      level2: { arr: null },
    }
  ).result;
}



const restructure = (data, keyName) => {
  let val = firstLevelRestructure(data)
  const emptyNode = {
    mName: "",
    aName: "",
    id: "",
    cName: "",
    gName: "",
    gAmount: "",
    gls: "",
    value: "",
    date: "",
    income: "",
    rows: [],
  };
  
  const groups = val.reduce((acc, item) => {
    acc[item[keyName]] ??= [];
    acc[item[keyName]].push({ ...item, [keyName]: "" });
    return acc;
  }, {});

  return Object.entries(groups)
    .map(([keyValue, rows]) => (
        { ...emptyNode,  [keyName]: keyValue , rows }
    ));  
};

console.log(JSON.stringify(restructure(data, 'mName')));
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

enter image description here

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

deleted 1258 characters in body
Source Link
SDK
  • 113
  • 4

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

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

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

function firstLevelRestructure(data){
  return data.reduce(
    (acc, row) => {
      if (row.advisorNameaName !== acc.level1.clientNamecName) {
        let newRow1 = {
          advisorNameaName: row.advisorNameaName,
          managerNamemName: row.managerNamemName,
          id: "",
          clientNamecName: "",
          goalNamegName: "",
          goalAmountgAmount: "",
          goalsgls: "",
          scorevalue: "",
          lastModifieddate: "",
          equityFixedIncomeincome: "",
          subRowsrows: [],
        };
        acc.result.push(newRow1);
        acc.level1.clientNamecName = row.advisorName;aName;
        acc.level1.arr = newRow1.subRows;rows;
      }
      let newRow2 = {
        advisorNameaName: "",
        managerNamemName: "",
        id: row.id,
        clientNamecName: row.clientNamecName,
        goalNamegName: row.goalNamegName,
        goalAmountgAmount: row.goalAmountgAmount,
        goalsgls: row.goalsgls,
        scorevalue: row.scorevalue,
        lastModifieddate: row.lastModifieddate,
        equityFixedIncomeincome: row.equityFixedIncomeincome,
      };
  
      if(row.subRowsrows) {
        acc.level2.arr = newRow2.subRowsrows = [];
      }
      acc.level1.arr.push(newRow2);
  
      if (row.subRowsrows) {
        row.subRowsrows.forEach((subRow) => {
          acc.level2.arr.push({ ...subRow });
        });
      }
      return acc;
    },
    {
      result: [],
      level1: { clientNamecName: "", arr: null },
      level2: { arr: null },
    }
  ).result;
}



const restructure = (data, keyName) => {
  let val = firstLevelRestructure(data)
  const emptyNode = {
    managerNamemName: "",
    advisorNameaName: "",
    id: "",
    clientNamecName: "",
    goalNamegName: "",
    goalAmountgAmount: "",
    goalsgls: "",
    scorevalue: "",
    lastModifieddate: "",
    equityFixedIncomeincome: "",
    subRowsrows: [],
  };
  
  const groups = val.reduce((acc, item) => {
    acc[item[keyName]] ??= [];
    acc[item[keyName]].push({ ...item, [keyName]: "" });
    return acc;
  }, {});

  return Object.entries(groups)
    .map(([keyValue, subRows]rows]) => (
        { ...emptyNode,  [keyName]: keyValue , subRowsrows }
    ));  
};

console.log(JSON.stringify(restructure(data, 'managerName''mName')));

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

enter image description here

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

enter image description here

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

function firstLevelRestructure(data){
  return data.reduce(
    (acc, row) => {
      if (row.aName !== acc.level1.cName) {
        let newRow1 = {
          aName: row.aName,
          mName: row.mName,
          id: "",
          cName: "",
          gName: "",
          gAmount: "",
          gls: "",
          value: "",
          date: "",
          income: "",
          rows: [],
        };
        acc.result.push(newRow1);
        acc.level1.cName = row.aName;
        acc.level1.arr = newRow1.rows;
      }
      let newRow2 = {
        aName: "",
        mName: "",
        id: row.id,
        cName: row.cName,
        gName: row.gName,
        gAmount: row.gAmount,
        gls: row.gls,
        value: row.value,
        date: row.date,
        income: row.income,
      };
  
      if(row.rows) {
        acc.level2.arr = newRow2.rows = [];
      }
      acc.level1.arr.push(newRow2);
  
      if (row.rows) {
        row.rows.forEach((subRow) => {
          acc.level2.arr.push({ ...subRow });
        });
      }
      return acc;
    },
    {
      result: [],
      level1: { cName: "", arr: null },
      level2: { arr: null },
    }
  ).result;
}



const restructure = (data, keyName) => {
  let val = firstLevelRestructure(data)
  const emptyNode = {
    mName: "",
    aName: "",
    id: "",
    cName: "",
    gName: "",
    gAmount: "",
    gls: "",
    value: "",
    date: "",
    income: "",
    rows: [],
  };
  
  const groups = val.reduce((acc, item) => {
    acc[item[keyName]] ??= [];
    acc[item[keyName]].push({ ...item, [keyName]: "" });
    return acc;
  }, {});

  return Object.entries(groups)
    .map(([keyValue, rows]) => (
        { ...emptyNode,  [keyName]: keyValue , rows }
    ));  
};

console.log(JSON.stringify(restructure(data, 'mName')));

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

Rollback to Revision 5
Source Link
Loading
added 25 characters in body
Source Link
SDK
  • 113
  • 4
Loading
added 6386 characters in body
Source Link
SDK
  • 113
  • 4
Loading
edited title
Link
SDK
  • 113
  • 4
Loading
added 90 characters in body
Source Link
SDK
  • 113
  • 4
Loading
edited tags
Link
SDK
  • 113
  • 4
Loading
added 98 characters in body
Source Link
SDK
  • 113
  • 4
Loading
Source Link
SDK
  • 113
  • 4
Loading