简体   繁体   English

按日期分组数据(年)Vue.js

[英]Group datas by date(year) Vue.js

I am trying to group my datas by date: dates with the same year belong to a group. 我正在尝试按日期对数据进行分组:同一年份的日期属于一个组。 i want to group invoices by data_emissione,which have the following structure: 我想按data_emissione将发票分组,其结构如下:

        invoices: [
         {id,data_emissione:"2017-06-19 00:00:00"},
         {id,data_emissione:"2017-05-19 00:00:00"},
         {id,data_emissione:"2018-06-24 00:00:00"}
        ]

, but the following method returns nothing. ,但以下方法未返回任何内容。

export default {
        data(){
            return {
                invoices:[],
                groups:{}

            }
        },
 mounted(){
            this.getMine();
            this.groupInvoices();
        },
 methods:{
            groupInvoices(){
                var self = this;
                var groups = _.groupBy(self.invoices,function(d){
                    return moment(d.data_emissione).year().format();
                });

            },

Any kind of help would be appreciated 任何帮助将不胜感激

Remove .format() and it should work: 删除.format() ,它应该可以工作:

export default {
    data(){
        return {
            invoices:[
                {data_emissione:"2017-06-19 00:00:00"},
                {data_emissione:"2017-05-19 00:00:00"},
                {data_emissione:"2018-06-24 00:00:00"}
            ],
            groups: {}
        }
    },
    mounted(){
        this.getMine();
        this.groupInvoices();
    },
    methods:{
        groupInvoices(){
            var self = this;
            var this.groups = _.groupBy(self.invoices,function(d){
                return moment(d.data_emissione).year();
            });

        },
    }
}

This is what I do most of the times: 这是我大多数时候做的事情:

  1. retrieve raw data 检索原始数据
  2. format them adding props 格式化他们添加道具
  3. manipulate formatted data 处理格式化的数据

You can also use lodash groupBy but most of the times I want an array or arrays and not an object of arrays. 您也可以使用lodash groupBy但是大多数时候我想要一个或多个数组而不是数组的对象。

 // retrieve let invoices = [ {id: 1, data_emissione: "2017-06-19 00:00:00"}, {id: 2, data_emissione: "2017-05-19 00:00:00"}, {id: 3, data_emissione: "2018-06-24 00:00:00"}] // format let formattedInvoices = invoices.map(elem => ({ id: elem.id, data_emissione: elem.data_emissione, year: moment(elem.data_emissione).year() })); // manipulate let groupedInvoices = [2017, 2018].map(year => formattedInvoices.filter(elem => elem.year === year)) console.log(groupedInvoices) 
 <script src="https://momentjs.com/downloads/moment.min.js"></script> 

An attempt to make previous answer better: 试图使先前的答案更好:

// retrieve
const invoices = [
    {id: 1, data_emissione: "2017-06-19 00:00:00"},
    {id: 2, data_emissione: "2017-05-19 00:00:00"},
    {id: 3, data_emissione: "2018-06-24 00:00:00"}]

  const groupedInvoices = {};

  invoices.forEach((invoice) => {
    const year = moment(invoice.data_emissione).year();
    groupedInvoices[year] = groupedInvoices[year] || [];
    groupedInvoices[year].push(invoice);
  });

Just use object with keys - years and avoid using another data array. 只需将对象与键一起使用-年,并避免使用另一个数据数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM