Search code examples
javascriptjqueryangularjsmongodbrestangular

Calculate redundancies with restangular


In my MongoDB i have a table named "Companies" its structure is like this :

"companies": [
    {
      "cityName": "Paris",
      "postalCode": "75002",
      "streetName": "7 rue des fourneaux",
      "name": "IB Consulting SA",
      "officeNumber": "",
      "createdAt": "2014-04-29T09:51:12.112Z",
      "updatedAt": "2014-04-29T09:51:12.112Z",
      "id": "65145648ddf514"
    },
...
]

I want to calculate the redundancies of "cityName" of all the companies in order to display it in a dxPieChart, i would like to use restangular, i m newbie with AngularJs, how can i do this please ?


Solution

  • After getting the Array by Restangular (how depends on if the array is a collection or a list of embedded documents because mongodb has no tables only documents and collections and how is the rest api route) u can get the count of the occurence of each city by using the lodash library

    var companies = ...

    var city_counts = _.countBy(companies, "cityName");

    the result is a object with properties of the city names and value the occurence e.g.

    city_counts ={"Paris":2,"Berlin":5}