繁体   English   中英

如何使用 javascript 按月获取对象的计数数组

[英]How to get a count array of objects by month with javascript

我需要一个由 12 个整数组成的数组,用于计算一年中的每个月有多少个元素属于以下数组,月份包含在可以作为字符串处理的 date 属性中。

[
    {
        "id": 1,
        "title": "Order 1",
        "date": "2020-06-15T18:05:43.040468",
        "type": "Affectation",
    },
    {
        "id": 2,
        "title": "Order 2",
        "date": "2020-06-15T18:05:55.397698",
        "type": "Affectation",
    },
    {
        "id": 3,
        "title": "Order 3",
        "date": "2020-05-15T18:06:15.853506",
        "type": "Affectation",
    },
    {
        "id": 4,
        "title": "Order 4",
        "date": "2020-03-15T18:06:44.421666",
        "type": "Affectation",
    },
    {
        "id": 5,
        "title": "Order 5",
        "date": "2020-05-15T18:06:44.421666",
        "type": "Affectation",
    },
    {
        "id": 6,
        "title": "Order 6",
        "date": "2020-01-15T18:07:03.856468",
        "type": "Affectation",
    },
    {
        "id": 7,
        "title": "Order 7",
        "date": "2020-02-15T20:09:25.164826",
        "type": "Affectation",
    },
    {
        "id": 8,
        "title": "Order 8",
        "date": "2020-03-15T20:09:25.164826",
        "type": "Affectation",
    }
]

结果将是这样的:

[1, 1, 2, 0, 2, 2, 0, 0, 0, 0, 0, 0]

您可以使用 forEach。 并通过使用 getMonth 每个月返回一个 integer 月份的索引。 然后递增数组中的相应月份。

// Create array of 12 items init'd at 0
// increment the count of each month
const monthCountArr = new Array(12).fill(0); 
data.forEach(({ date }) => monthCountArr[new Date(date).getMonth()] += 1);

console.log(monthCountArr);

我决定试一试,这里是代码。

const orderData = [
    {
        id: 1,
        title: 'Order 1',
        date: '2020-06-15T18:05:43.040468',
        type: 'Affectation',
    },
    {
        id: 2,
        title: 'Order 2',
        date: '2020-06-15T18:05:55.397698',
        type: 'Affectation',
    },
    {
        id: 3,
        title: 'Order 3',
        date: '2020-05-15T18:06:15.853506',
        type: 'Affectation',
    },
    {
        id: 4,
        title: 'Order 4',
        date: '2020-03-15T18:06:44.421666',
        type: 'Affectation',
    },
    {
        id: 5,
        title: 'Order 5',
        date: '2020-05-15T18:06:44.421666',
        type: 'Affectation',
    },
    {
        id: 6,
        title: 'Order 6',
        date: '2020-01-15T18:07:03.856468',
        type: 'Affectation',
    },
    {
        id: 7,
        title: 'Order 7',
        date: '2020-02-15T20:09:25.164826',
        type: 'Affectation',
    },
    {
        id: 8,
        title: 'Order 8',
        date: '2020-03-15T20:09:25.164826',
        type: 'Affectation',
    },
];

// This function takes a string and creates a date object from the string and return the month. 
const findMonth = (datestring) => {
    let date = new Date(datestring);
    return date.getMonth();
};

const makeDateArray = (orders) => {
    let monthFreq = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // you can use Array.fill() but for clarity
    // now you loop over each object in the data array
    for (const order of orders) {
        const month = parseInt(findMonth(order.date));
        // Months are from 0-11 in JS so it matches directly with array index
        monthFreq[month] = monthFreq[month] + 1;
    }
    return monthFreq;
};

console.log(makeDateArray(orderData));

结果 -

结果

将来,至少试一试,问你卡在哪里的问题,而不是只问零的问题! GL!

暂无
暂无

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

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