Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 138134

How to merge keys in Object Array

$
0
0

I have an Array with objects in it like that:

[
  {
    employee: "John Doe",
    customer: "Customer 1",
    startdate: '2020-02-10T11:54:00.000Z',
    enddate: '2020-05-10T11:54:00.000Z'
  },
  {
    employee: "John Doe",
    customer: "Customer 2",
    startdate: '2020-02-10T11:57:00.000Z',
    enddate: '2020-02-12T11:57:00.000Z'
  },
  {
    employee: "Susan Doe",
    customer: "Customer 2",
    startdate: '2020-02-10T11:57:00.000Z',
    enddate: '2020-02-12T11:57:00.000Z'
  },
  {
    employee: "Susan Doe",
    customer: "Customer 2",
    startdate: '2020-02-25T08:22:00.000Z',
    enddate: '2020-02-28T08:22:00.000Z'
  }
]

As you can see there are two different employees and two different customers. How can i merge this array so i get something like this:

[
  {
    employee: "John Doe",
    customer: "Customer 1",
    plans: [
      {
        startdate: '2020-02-10T11:54:00.000Z',
        enddate: '2020-05-10T11:54:00.000Z'
      }
    ]
  },
  {
    employee: "John Doe",
    customer: "Customer 2",
    plans: [
      {
        startdate: '2020-02-10T11:54:00.000Z',
        enddate: '2020-05-10T11:54:00.000Z'
      }
    ]
  },
  {
    employee: "Susan Doe",
    customer: "Customer 2",
    plans: [
      {
        startdate: '2020-02-10T11:57:00.000Z',
        enddate: '2020-02-12T11:57:00.000Z'
      },
      {
        startdate: '2020-02-25T08:22:00.000Z',
        enddate: '2020-02-28T08:22:00.000Z'
      }
    ]
  }
]

The Employees should have a seperate startdate and enddate array for each customer


Viewing all articles
Browse latest Browse all 138134

Trending Articles