如何使用具有不同值的nunjucks groupby?

How to use nunjucks groupby with different value?

当我有这种值时,我无法弄清楚如何从 Json 中获取我的数据

    2017-10-16 12:07:07
2017-10-16 12:07:07
2017-10-16 15:09:08
2017-10-16 15:09:08
2017-10-16 15:09:08
2017-10-16 15:09:08
2017-10-16 15:09:08
2017-10-16 18:11:09
2017-10-16 18:11:09
2017-11-29 17:26:57
2017-11-29 17:26:57
2017-11-29 17:26:57
2017-11-29 19:31:03

我的模板

 <div class="container">
      <div class="row">
          <div class="col-5">
               {% for date , block in block | groupby("date") %}
                 <div class="date">{{date |date }}</div>
              {% endfor %}

          </div>
      </div>
  </div>

在输出中我有

16 Oct
16 Oct
16 Oct
3 Nov
29 Nov
29 Nov

我想要

  16 Oct
  3 Nov
  29 Nov

可能是因为起初我从 Json 获取了所有数据,然后我将其分组,然后才使用我的 dateFilter ,但我怎样才能以不同的方式获取它?

我的gulpfile.js gulpfile

更新 19.12.18

<div class="container">
       <div class="row">
            {% for date, block in block | groupby("date", "datefilter") %}
           <div class="col-5">
                <div class="date">
                        <h3> {{date}}</h3>
                        <p>amount {{block.length}}</p> // this line will give me amount of products for every day . 
                    </div>
              {% for id, block in block | groupby("id") %}
               <div class="id"> 
              {% for name, block in block | groupby("name") %}
                <p>{{name}} № {{ id }}  </p>
             </div>
               {% endfor %}
            {% endfor %}
           </div>

            {% endfor %}
       </div>
   </div>

example output

您的分组依据多次返回 16 Oct,因为您在该日期列出了不同的时间值。如果您首先将日期格式化为仅显示日期而不显示时间,则您的分组依据应该可以工作。您可以尝试仅提取飞镖部分 - 如果您愿意,可以将语言环境字符串替换为您喜欢的其他字符串。

var date = new Date("2017-11-29 19:31:03").toLocaleDateString("en-US");
console.log(date);

下面我将标准 groupby(prop) 过滤器改进为 groupby(prop, filter)

在我的代码中,我使用 roundDate 代替日期舍入过滤器的 date 名称。

var nunjucks  = require('nunjucks');
var env = nunjucks.configure();

env.addFilter('roundDate', (e) => e.substring(0, 7));

env.addFilter('groupby', function (arr, prop, filter) {
    var res = {};
    var iterator = typeof prop == 'function' ? prop : (e) => e[prop];
    var func = this.env.filters[filter] || ((e) => e);
    arr.forEach(function (e, i) {
        var key = func(iterator(e, i));
        (res[key] || (res[key] = [])).push(e);
    })
    return res;
})

var data = [
    {date: "2017-10-16 12:07:07", value: "123"},
    {date: "2017-10-16 12:07:07", value: "123"},
    {date: "2017-10-16 15:09:08", value: "123"},
    {date: "2017-10-16 15:09:08", value: "123"},
    {date: "2017-10-16 15:09:08", value: "123"},
    {date: "2017-10-16 15:09:08", value: "123"},
    {date: "2017-10-16 15:09:08", value: "123"},
    {date: "2017-10-16 18:11:09", value: "123"},
    {date: "2017-10-16 18:11:09", value: "123"},
    {date: "2017-11-29 17:26:57", value: "123"},
    {date: "2017-11-29 17:26:57", value: "123"},
    {date: "2017-11-29 17:26:57", value: "123"},
    {date: "2017-11-29 19:31:03", value: "123"}
]

var res = env.renderString(`
    {% for a, items in data | groupby('date', 'roundDate') %}
       {{a}} {{items.length}}
    {% endfor %}
    `,
    {data}
);

console.log(res);

我喜欢这个,对吗?

var manageEnvironment = function(environment) {

        environment.addFilter('datefilterq', dateFilter);
        dateFilter.setDefaultFormat('D MMM');
        environment.addFilter('groupby', function (arr, prop, filter) {
          var res = {};
          var iterator = typeof prop == 'function' ? prop : (e) => e[prop];
          var func = this.environment.filters[filter] || ((e) => e);
          arr.forEach(function (e, i) {
              var key = func(iterator(e, i));
              (res[key] || (res[key] = [])).push(e);
          })
          return res;
        })


}