angularjs - 是否可以用日期索引 Angularjs 中的表?

标签 angularjs indexing

我正在根据两个系列的日期创建一个图表:

  • 创建的项目数量
  • 更新的项目数量

我从我们的 API 发出了请求,并得到了一个包含以下数据的文件:

"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"
}
{...}
]

我想知道获取图形数据作为这种类型的对象集合的最佳解决方案是什么:

x : date,
created : integer,
updated : integer

我认为应该有一个以日期作为索引的表,然后遍历整个文件并增加索引表中的值。

但是,我在互联网上查找信息时遇到了一些问题。 我想到了类似的事情:

  $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);


  }

但是,它不起作用..有什么建议吗? 谢谢!

最佳答案

您可以尝试使用reduce function 。像这样的事情:

$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;

}, []);

关于angularjs - 是否可以用日期索引 Angularjs 中的表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32512152/

相关文章:

javascript - 将 Angular $asyncValidators 与缓存一起使用

javascript - 需要循环 json 键值对的帮助

mysql - 非常简单的自连接,却被忽略了?

python - 使用 bool 矩阵替换 for 循环以执行高级索引

apache - 从 Apache solr 中提取内容摘录

symfony - 在 Symfony 2 中设置索引路由

javascript - Angular Kendo 调度器过滤器

javascript - <br> Angular 表达之间

javascript - Angular-route1.6.1 无法正常工作

algorithm - 位串上 top-k 查询的索引结构