如何在打字稿/离子2中按周分组时间戳?

Posted

技术标签:

【中文标题】如何在打字稿/离子2中按周分组时间戳?【英文标题】:How to group timestamp by week in typescript / ionic 2? 【发布时间】:2017-12-20 17:59:24 【问题描述】:

我已将以下 json 放入一个名为 logs 的数组中,并希望使用创建的时间戳对每周进行分组。


  created : "2017-07-15 09:49:37"
  entry : "this is a log entry"

  created : "2017-07-13 09:49:37"
  entry : "this is a log entry"


  created : "2016-05-13 11:00:21"
  entry : "this is another log entry"

还有 ionic 2 html

<div *ngFor="let log of logs">
      <ion-list-header>
          log.created
      </ion-list-header>
     <ion-item>
         log.entry
     </ion-item>
</div>

编辑

好的,我现在已经在 php 中完成了每周分组,它返回以下 Json,

"payload": 
"logs": 
  "19": [
    "[\"entry\":"this is an entry",\"created\":\"2017-05-12 09:51:18\"]"
  ],
  "28": [
    "[\"entry\":"this is an entry",\"created\":\"2017-07-15 09:50:50\"]",
    "[\"entry\":"this is an entry",\"created\":\"2017-07-15 09:51:18\"]"
  ]

我是这样收到的,

private weeks: any[];

this.api.getLogs().subscribe(
      data => 
        // Success
        this.weeks = data.payload.logs;
      ,
      err => 
        //Err
      
    );

我尝试在 Ionic 2 中使用它,

<div *ngFor="let week of weeks">
    <div *ngFor="let log of week">
        log.created
    </div>
</div>

但这给了我以下错误,

Runtime Error
Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.

任何帮助表示赞赏。

谢谢

【问题讨论】:

您能告诉我们到目前为止您尝试了什么吗? 嗨,我已经编辑了帖子以显示我目前的情况。我在 php 方面按周分组,但在将 json 放入列表时遇到问题。 【参考方案1】:

您将 weeks 声明为任意 (private weeks: any[];) 的数组,但您使用 JSON 设置它(data.payload.logs 是 JSON),因此您似乎在这里至少有一个类型错误。

【讨论】:

以上是关于如何在打字稿/离子2中按周分组时间戳?的主要内容,如果未能解决你的问题,请参考以下文章

Graphite 组时间戳事件(按周)

SQLAlchemy 在时间戳列中按天分组

我需要从结构为 yyyyMMddHHmmss 20170227141500 的时间戳中按年和月分组

在按周分组的 2 个日期之间在 MYSQL 中生成报告

如何获得最近 x 周数据的不同计数,但在红移中按周分组?

离子 2 - 打字稿 |填充接口对象数组