提问者:小点点

没有销售的月份怎么加0?


我有一个数组:

  let sales=  [
       { _id: { year: 2020, month: 3 }, total_sales: 1800 },
       { _id: { year: 2020, month: 4 }, total_sales: 5700 },
       { _id: { year: 2020, month: 5 }, total_sales: 6630 },
       { _id: { year: 2020, month: 6 }, total_sales: 690 },
       { _id: { year: 2020, month: 7 }, total_sales: 1600 },
       { _id: { year: 2020, month: 8 }, total_sales: 13430 }
     ]

我要做的是,通过

            let months = []
            let sales = []
            var m = ["January", "February", "March", "April", "May", "June",
                "July", "August", "September", "October", "November", "December"]

            for (let i = 0; i < sales.length; i++) {
                if (sales.length === 6) {
                    var monthName = m[sales[i]._id.month - 1]
                    months.push(monthName)
                    sales.push(sales[i].total_sales)
                }
            }

按预期返回以下内容。

["March", "April", "May", "June", "July", "August"] 
[1800, 5700, 6630, 690, 1600, 13430]

但是,如果我在数组中缺少一个月,我如何重新排列它以显示带有monthName的total_sales=0

显示事例:

 [
       { _id: { year: 2019, month: 12 }, total_sales: 1800 },
       { _id: { year: 2020, month: 1 }, total_sales: 5700 },
       { _id: { year: 2020, month: 2 }, total_sales: 6630 },
       { _id: { year: 2020, month: 4 }, total_sales: 690 },
       { _id: { year: 2020, month: 6 }, total_sales: 1600 },
       { _id: { year: 2020, month: 8 }, total_sales: 13430 }
     ]

预期结果:

["August","September","October","November",
"December","January","February","March", "April", 
"May", "June", "July", "August"] 

[0, 0, 0, 0, 1800, 5700, 6630, 0, 690, 0, 1600, 0, 13430]

Every 12 month period.

共2个答案

匿名用户

我在想这样的事情:

const initialArray = [
   { _id: { year: 2020, month: 3 }, total_sales: 1800 },
   { _id: { year: 2020, month: 4 }, total_sales: 5700 },
   { _id: { year: 2020, month: 5 }, total_sales: 6630 },
   { _id: { year: 2020, month: 6 }, total_sales: 690 },
   { _id: { year: 2020, month: 7 }, total_sales: 1600 },
   { _id: { year: 2020, month: 8 }, total_sales: 13430 }
 ]
 
 const allMonths = {
    '1': 'January',
    '2': 'February',
    '3': 'March',
    '4': 'April',
    '5': 'May',
    '6': 'June',
    '7': 'July',
    '8': 'August',
    '9': 'September',
    '10': 'October',
    '11': 'November',
    '12': 'December',
 }
 
const months = []
const sales = []

for (const monthNum in allMonths) {
    monthData = initialArray.find(month => month._id.month == monthNum)
    months.push(allMonths[monthNum])
    sales.push(monthData ? monthData.total_sales : 0)
}

匿名用户

下面的怎么样

    let months = []
    let sales = []
    var m = ["January", "February", "March", "April", "May", "June",
        "July", "August", "September", "October", "November", "December"]
    
    const firstEntry = this.props.salesGraph.sales[0]._id;
    
    const startMonth = firstEntry.year*12+firstEntry.month;
    const lastIteratedMonth = startMonth-1;
    for (let i = 0; i < this.props.salesGraph.sales.length; i++) {
        let currentEntry = this.props.salesGraph.sales[i]._id;
        let currentMonth = currentEntry.year*12+currentEntry.month;
        while(lastIteratedMonth+1 < currentMonth) {
            //Fill missing months with 0
            var monthName = m[lastIteratedMonth%12-1];
            months.push(monthName);
            sales.push(0);
            lastIteratedMonth++;
        }
        let monthName = m[currentEntry.month - 1];
        months.push(monthName);
        sales.push(currentEntry.total_sales);
        lastIteratedMonth++;
    }