Home > Back-end >  How to create new array of nested object form an existing array of object
How to create new array of nested object form an existing array of object

Time:12-02

Please help to restructure the object in js.

const o = [
  {
    id: 1612,
    Tasks: 'Remuneration sheet mentain.',
    calendar_year: 2021,
    calendar_week_in_year: 45,
    WeekStartDate: '2021-10-31',
    WeekEndDate: '2021-11-06',
    status: 'Done',
  },
  {
    id: 1612,
    Tasks: 'Remuneration sheet mentain.',
    calendar_year: 2021,
    calendar_week_in_year: 46,
    WeekStartDate: '2021-11-28',
    WeekEndDate: '2021-12-04',
    status: 'Done',
  },
  {
    id: 1939,
    Tasks: 'Debtors Reconciliation',
    calendar_year: 2021,
    calendar_week_in_year: 45,
    WeekStartDate: '2021-10-31',
    WeekEndDate: '2021-11-06',
    status: 'Done',
  },
  {
    id: 1939,
    Tasks: 'Debtors Reconciliation',
    calendar_year: 2021,
    calendar_week_in_year: 46,
    WeekStartDate: '2021-11-28',
    WeekEndDate: '2021-12-04',
    status: 'Done',
  },
];

const required = [
  {
    id: 1612,
    Tasks: 'Remuneration sheet mentain.',
    week: [
      { w_no: 45, calendar_year: 2021, WeekStartDate: '2021-10-31', WeekEndDate: '2021-11-06', status: 'Done' },
      { w_no: 46, calendar_year: 2021, WeekStartDate: '2021-11-28', WeekEndDate: '2021-12-04', status: 'Done' },
    ],
  },
  {
    id: 1939,
    Tasks: 'Debtors Reconciliation',
    week: [
      { w_no: 45, calendar_year: 2021, WeekStartDate: '2021-10-31', WeekEndDate: '2021-11-06', status: 'Done' },
      { w_no: 46, calendar_year: 2021, WeekStartDate: '2021-11-28', WeekEndDate: '2021-12-04', status: 'Done' },
    ],
  },
];

CodePudding user response:

I think what you are trying to do is a nested array?

Im not entirely sure what you are trying to achieve as the code is not formated correctly however try and google search Nested Arrays.

Should be on the lines of something like this:

{id:   ,Task:    ,Week:[]}

CodePudding user response:

This is really just a 'group-by' grouping by id and accumulating the relevant properties in an array. Here using reduce() and destructuring to isolate the necessary properties of the iterated objects.

const o = [{ id: 1612, Tasks: 'Remuneration sheet mentain.', calendar_year: 2021, calendar_week_in_year: 45, WeekStartDate: '2021-10-31', WeekEndDate: '2021-11-06', status: 'Done', }, { id: 1612, Tasks: 'Remuneration sheet mentain.', calendar_year: 2021, calendar_week_in_year: 46, WeekStartDate: '2021-11-28', WeekEndDate: '2021-12-04', status: 'Done', }, { id: 1939, Tasks: 'Debtors Reconciliation', calendar_year: 2021, calendar_week_in_year: 45, WeekStartDate: '2021-10-31', WeekEndDate: '2021-11-06', status: 'Done', }, { id: 1939, Tasks: 'Debtors Reconciliation', calendar_year: 2021, calendar_week_in_year: 46, WeekStartDate: '2021-11-28', WeekEndDate: '2021-12-04', status: 'Done', },];

const result = Object.values(
  o.reduce((a, { id, Tasks, ...week }) => {
    (a[id] ??= { id, Tasks, week: [] }).week.push(week);

    return a;
  }, {})
);

console.log(result);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related