-1

Please see raw dataset below:

[
    {
        "date": "2021-12-03",
        "minutes": 132.00001,
        "category": "RSVR"
    },
    {
        "date": "2021-12-03",
        "minutes": 4.95,
        "category": "DMND"
    },
    {
        "date": "2021-12-03",
        "minutes": 127.03218,
        "category": "SLIP"
    },
    {
        "date": "2021-12-04",
        "minutes": 113.97533,
        "category": "SLIP"
    },
    {
        "date": "2021-12-04",
        "minutes": 11.55,
        "category": "DMND"
    },
    {
        "date": "2021-12-04",
        "minutes": 105.30001,
        "category": "RSVR"
    }
]

Expected:
would like generate new array where:

  • objects with category: 'DMND' are added as it is
  • new object is created using structure (example of new object provided below):
    • {
        date: unique date from the objects with  category: 'RSVR' and category: 'SLIP'
        minutes: add minutes from category: 'RSVR' and category: 'SLIP'
        category: 'SPLY'
      }
      
      Example
      
      {
        "date": "2021-12-03",
        "minutes": 132.00001,
        "category": "RSVR"                                 {
      },                                                     "date": "2021-12-03",
      {                                            =         "minutes": 259.03219,
        "date": "2021-12-03",                                "category": "SPLY"
        "minutes": 127.03218,                              }
        "category": "SLIP"
      }
      
    (basically merging 2 objects with category: 'RSVR' and category: 'SLIP' by adding their minutes to create a new object and add that new object to the array)
New array will look like this:
[
    {
        "date": "2021-12-03",
        "minutes": 4.95,
        "category": "DMND"
    },
    {
        "date": "2021-12-03",
        "minutes": 259.03219,
        "category": "SPLY"
    },
    {
        "date": "2021-12-04",
        "minutes": 11.55,
        "category": "DMND"
    },
    {
        "date": "2021-12-04",
        "minutes": 219.275331,
        "category": "SPLY"
    }
]

Not quite sure how to go about doing this yet. any help would be appreciated

user10368959
  • 155
  • 3
  • 14
  • 1
    A loop (e.g. `for`) an `if` and `.find()` or a second loop should get you at least into the right direction. – Andreas Dec 06 '21 at 15:04
  • I assume you know how to [clone simple objects](https://stackoverflow.com/questions/728360/how-do-i-correctly-clone-a-javascript-object). You might want to edit the question to include only your actual question ("basically merging 2 objects [...] by adding their minutes") to increase the chances of getting an answer. – Michael T Dec 06 '21 at 15:04

2 Answers2

2

I am sure there might be a simpler solution, but this will work using reduce and combining if it finds one or the other.

var inputs = [{
    "date": "2021-12-03",
    "minutes": 132.00001,
    "category": "RSVR"
  },
  {
    "date": "2021-12-03",
    "minutes": 4.95,
    "category": "DMND"
  },
  {
    "date": "2021-12-03",
    "minutes": 127.03218,
    "category": "SLIP"
  },
  {
    "date": "2021-12-04",
    "minutes": 113.97533,
    "category": "SLIP"
  },
  {
    "date": "2021-12-04",
    "minutes": 11.55,
    "category": "DMND"
  },
  {
    "date": "2021-12-04",
    "minutes": 105.30001,
    "category": "RSVR"
  }
];

var results = Object.values(inputs.reduce((o, item) => {
  o[item.date] = o[item.date] || {};
  if (["RSVR", "SLIP"].includes(item.category)) {
    const opp = item.category === "SLIP" ? "RSVR" : "SLIP";
    if (o[item.date][opp]) {
      o[item.date][opp].minutes += item.minutes;
      o[item.date][opp].category = 'SPLY';
      return o;
    }
  }
  o[item.date][item.category] = item
  return o;
}, {})).flatMap(x => Object.values(x));
console.log(results);
epascarello
  • 204,599
  • 20
  • 195
  • 236
0

We can use Array.reduce to group the entries by date and category. We can use a categoryMap to map input to output categories.

If the output categories match, we'll just sum up minutes for each entry.

let input = [ { "date": "2021-12-03", "minutes": 132.00001, "category": "RSVR" }, { "date": "2021-12-03", "minutes": 4.95, "category": "DMND" }, { "date": "2021-12-03", "minutes": 127.03218, "category": "SLIP" }, { "date": "2021-12-04", "minutes": 113.97533, "category": "SLIP" }, { "date": "2021-12-04", "minutes": 11.55, "category": "DMND" }, { "date": "2021-12-04", "minutes": 105.30001, "category": "RSVR" } ]

const categoryMap =  { DMND: 'DMND', RSVR: 'SPLY', SLIP: 'SPLY' };

const result = Object.values(input.reduce((acc, { date, minutes, category }) => {
    const key = `${date}-${categoryMap[category]}`;
    acc[key] = acc[key] || { date, category: categoryMap[category], minutes: 0 };
    acc[key].minutes += minutes;
    return acc;
}, {}));

console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40