Is it possible to index a table in Angularjs with dates?

I am working on creating a graph depending on the date with two series:

  • number of items created
  • updated number of items

I made a request from our API and received a file that has the following data:

"result":[
{
"closed_on" : "YYYY-MM-DD",
"created_on" : "YYYY-MM-DD"
}
{
"closed_on" : "YYYY-MM-DD",
"created_on" : "YYYY-MM-DD"
}
{
"closed_on" : "YYYY-MM-DD",
"created_on" : "YYYY-MM-DD"
}
{...}
]

I was wondering what is the best solution for getting data for a graph as a collection of objects of this type:

x : date,
created : integer,
updated : integer

I thought that you need to have a table with a date as an index and iterate over the whole file and increase the value in the indexed table.

However, I had some problems finding information on the Internet. I thought of something like this:

  $scope.datas=[];

  $scope.date = $scope.date.start;

  while($scope.date!==$scope.date.end) 
  {
      $scope.datas.push({
      date
      : [{
        closed_tickets:0,
        created_tickets:0
      }]
    });

      $scope.date = $scope.date.addDays(1);


  }

However, this does not work. Any suggestion? Thanks!

+4
source share
1 answer

. - :

$scope.dateList = response.result //get the array of dates

$scope.datas = $scope.dateList.reduce(function(prev, current, index, array) {
 var closedIndex = prev.findIndex(function(ele, i, a) {ele.x == current.closed_on}); 
 if(closedIndex > -1) 
   prev[closedIndex].closed_tickets++;
 else
   prev.push({x:current.closed_on, closed_tickets:1, created_tickets:0});

 var createdIndex = prev.findIndex(function(ele, i, a) {ele.x == current.created_on}); 
 if(createdIndex > -1) 
   prev[createdIndex ].created_tickets++;
 else
   prev.push({x:current.created_on, closed_tickets:0, created_tickets:1});

 return prev;

}, []);
+2

Source: https://habr.com/ru/post/1606872/


All Articles