簡體   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