Home > OS >  Combine JavaScript date object in an array of objects
Combine JavaScript date object in an array of objects

Time:01-11

I am working on app and I want to show 3 columns

date, min and zone

I am getting an array of objects

[
{date: 2022-12-28T07:37:16.859Z, min: 2, zone: zone A},
{date: 2022-12-28T07:38:13.859Z, min: 1, zone: zone B},
{date: 2022-12-28T07:36:15.859Z, min: 3, zone: zone C},
{date: 2022-12-31T07:37:16.859Z, min: 2, zone: zone E}
{date: 2022-12-25T07:37:16.859Z, min: 4, zone: zone D}
]

I want to cover condition as below:

  1. If date is repeating then it should be repeated only once.
  2. min with same date should be addition like (2 1 3=6)
  3. zone with same date should be concat like (Zone A, Zone B, Zone C)

and want result like below:

   [
    {date: 2022-12-28T07:37:16.859Z, min: 6, zone: zone A, zone B, zone C},
    {date: 2022-12-31T07:37:16.859Z, min: 2, zone: zone E}
    {date: 2022-12-25T07:37:16.859Z, min: 4, zone: zone D}
    ]

any help is apricated.

CodePudding user response:

Filter out elements with a duplicate date, and when you remove them add/concat the min/zone values to the remaining object's respective values.

How to remove duplicates

CodePudding user response:

First, you need to format date according to manage condition in code

a = [
  {date: "2022-12-28", min: 2, zone: "zone A"},
  {date: "2022-12-28", min: 1, zone: "zone B"},
  {date: "2022-12-28", min: 3, zone: "zone C"},
  {date: "2022-12-31", min: 2, zone: "zone E"},
  {date: "2022-12-25", min: 4, zone: "zone D"}
]

let h = []
a.forEach(item => {
    let data = h.find(e => e.date == item.date);
    if(data){
        data.min = data.min   item.min;
        data.zone = data.zone   ", "   item.zone;
        let index = h.findIndex(e => e.date == item.date);
        h[index] = data
    }else
        h.push(item)
})
console.log(h)

CodePudding user response:

The desired result requires grouped by date (with Date only).

You can work with .reduce() to group the data by date.

  1. When there is no element with date found in the group, you need to create an object with k and v. Next push it into the accumulator (acc) array.

  2. When there is an element with date match in the existing group, you need to update the existing element in the acc array for the min and zone.

Lastly, you need to grab all the v values from the groupedDate array.

let input = [
    { date: "2022-12-28T07:37:16.859Z", min: 2, zone: "zone A" },
    { date: "2022-12-28T07:38:13.859Z", min: 1, zone: "zone B" },
    { date: "2022-12-28T07:36:15.859Z", min: 3, zone: "zone C" },
    { date: "2022-12-31T07:37:16.859Z", min: 2, zone: "zone E" },
    { date: "2022-12-25T07:37:16.859Z", min: 4, zone: "zone D" }
];

let groupedDate = input.reduce(
    (acc: { k: string, v: { date: string; min: number; zone: string[] } }[], 
    cur: { date: string, min: number, zone: string }) => {

    let date = new Date(cur.date);
    let dateString = `${date.getFullYear()}-${date.getMonth()   1}-${date.getDate()}`;

    let index = acc.findIndex(x => x.k == dateString);
    if (index > -1) {
        acc[index].v.min  = cur.min;
        acc[index].v.zone.push(cur.zone);
    } else {
        acc.push({
            k: dateString,
            v: { date: cur.date, min: cur.min, zone: [cur.zone] }
        });
    }

    return acc;
}, []);

let result = groupedDate.map(x => x.v);

Demo @ TypeScript Playground

CodePudding user response:

you can use the Array.map() method to create a new array of objects that combine a JavaScript Date object with other properties.

let dateArray = [new Date(), new Date(), new Date()];

let dataArray = dateArray.map(date => {
  return {
    date: date,
    value: Math.random()
  }
});
  • Related