0

we have data returned from rest service in below JSON format and I want to group data by ActivityStartDate and then show in format. I can iterate through and create group by ActivityStartDate but this seems not efficient method in case we have more items returned.

Is there any better way to achieve it?

{
  "results": [
    {
      "Id": 1,
      "Title": "Food Promotion - 1",
      "ActivityStartDate": "2015-12-12T08:00:00Z",
      "ActivityEndDate": "2016-01-12T08:00:00Z",
      "ActivityDescription": "Two for one if dream and do not dream of fack promotions",
      "ShowInHistory": true,
      "ShowUpdated": true,
      "Modified": "2015-12-14T21:28:37Z",
      "Created": "2015-12-14T21:28:37Z"
    }
  ]
}
Milan
  • 1,903
  • 17
  • 16
oypatel
  • 73
  • 1
  • 11
  • 1
    Does the REST API you're using allow you to query by group? If not, you may just have to write a function to iterate over the data. – Jeremy Jackson Dec 15 '15 at 23:03
  • http://stackoverflow.com/questions/14446511/what-is-the-most-efficient-method-to-groupby-on-a-javascript-array-of-objects – makeitmorehuman Dec 15 '15 at 23:23

1 Answers1

0

Reason to group in front end

Assuming you can get a collection from the back end by GET /activities. If the returned JSON array is either small or pre-sorted it may be just more flexible to group late (in front end). This labor division between front and back end follows separation of concerns:

terms front end and back end refer to the separation of concerns between the presentation layer (front end), and the data access layer (back end)

Limitations: Data should be small because then it can be completely fetched and independently sorted in the front end. Contrary big data can lead to performance issues when loading/grouping. Thus it should be pre-sorted on the server, which can be achieved if your REST API (back end) supports sorting/paging for collection resources.

How to group in Angular

See typescript1.7 - How to group data in Angular 2? - Stack Overflow with advice on Angular's Pipes demonstrated on Plunkr in answer.

Similar for older AngularJS there is a Filter component achieving the same goal: transformation/grouping/filtering of data.

hc_dev
  • 8,389
  • 1
  • 26
  • 38