1

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.

chotya
  • 91
  • 5

4 Answers4

0

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

Jash1395
  • 228
  • 1
  • 6
0

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)
Rustam Ansari
  • 117
  • 12
0

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

Yong Shun
  • 35,286
  • 4
  • 24
  • 46
-2

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()
  }
});
Ikkhan
  • 16
  • 2
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 12 '23 at 05:33