1

I have an array of objects such as:

const array = [
  {
    date: '02-02-1994',
    time: '18:00',
    services: {
      first: 1,
      second: 1
    }
  },
  {
    date: '02-02-1994',
    time: '20:00',
    services: {
      first: 1,
      second: 1
    }
  },
  {
    date: '02-04-1994',
    time: '19:00',
    services: {
      first: 1,
      second: 1
    }
  },
{
  date: '02-04-1994',
  time: '19:00',
  services: {
    first: 1,
    second: 2
  }
}
]

I want to group it by time and date and get the result array:

const result = [{
  date: '02-02-1994',
  time: '18:00',
  services: {
    first: 1,
    second: 1
  }
},
{
  date: '02-02-1994',
  time: '20:00',
  services: {
    first: 1,
    second: 1
  }
},
{
  date: '02-04-1994',
  time: '19:00',
  services: {
    first: 2,
    second: 3
  }
}] 

I want to group array by date then by time and to get a sum in service object. I try to group by date using reduce, but then I don't know

2
  • 4
    please add your try. Commented Sep 10, 2019 at 20:36
  • I grouped by date jsfiddle.net/atcr04vm But then I don't know... Commented Sep 10, 2019 at 20:55

2 Answers 2

1

You can use reduce() method to do that.

const array = [{ date: '02-02-1994', time: '18:00', services: { first: 1, second: 1 } }, { date: '02-02-1994', time: '20:00', services: { first: 1, second: 1 } }, { date: '02-04-1994', time: '19:00', services: { first: 1, second: 1 } }, { date: '02-04-1994', time: '19:00', services: { first: 1, second: 2 } } ];

let result = array.reduce((arr, currentValue) => {

  let item = arr.find(item =>
    item.date === currentValue.date &&
    item.time === currentValue.time);

  if (item) {
    item.services.first += currentValue.services.first;
    item.services.second += currentValue.services.second;
  } else {
    arr.push(currentValue);
  }

  return arr;
}, []);

console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

1

You could take a joined key for collecting items of the same group in an object and then take the values of it as result set.

var array = [{ date: '02-02-1994', time: '18:00', services: { first: 1, second: 1 } }, { date: '02-02-1994', time: '20:00', services: { first: 1, second: 1 } }, { date: '02-04-1994', time: '19:00', services: { first: 1, second: 1 } }, { date: '02-04-1994', time: '19:00', services: { first: 1, second: 2 } }],
    result = Object.values(array.reduce((r, { date, time, services }) => {
        var key = [date, time].join('|');
        r[key] = r[key] || { date, time, services: {} };
        Object
            .entries(services)
            .forEach(([k, v]) => r[key].services[k] = (r[key].services[k] || 0) + v);
        return r;
    }, {}));

   console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.