You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JS对象格式化:按年/月/周统计数据总量

Solution to Convert Nested Date Data to Year/Month/Week Summary

Here's a straightforward approach to transform your nested JavaScript data into the required summary format, perfect for charting:

Step-by-Step Explanation

  1. Traverse the Nested Structure: Iterate through each level of your input object (year → month → week) to extract key values.
  2. Convert Month Numbers to Short Names: Use the built-in Intl.DateTimeFormat to reliably generate abbreviated month names (like "Jan" for "01") without hardcoding.
  3. Count Entries per Week: For each week, the total number of date entries is simply the length of the date array in that week's data.
  4. Build Summary Objects: Construct each summary entry with parsed year, formatted month name, week number, and entry count, then add it to the result array.

Complete Code

// Helper function to get short month name (e.g., "Jan" from "01")
function getShortMonthName(monthStr) {
    // Date objects use 0-based months, so subtract 1 from the parsed month string
    const date = new Date(2023, parseInt(monthStr) - 1, 1);
    return new Intl.DateTimeFormat('en-US', { month: 'short' }).format(date);
}

// Your input data
const inputData = { 
    "2011":{ 
        "01":{ 
            "01":[{ "date":"2011-01-01" },{ "date":"2011-01-02" }] 
        }, 
        "02":{ 
            "01":[{ "date":"2011-02-02" }], 
            "03":[{ "date":"2011-02-15" },{ "date":"2011-02-17" }] 
        } 
    }, 
    "2012":{ 
        "01":{ 
            "01":[{ "date":"2012-01-01" }] 
        }, 
        "03":{ 
            "01":[{ "date":"2012-03-03" }] 
        } 
    } 
};

// Initialize the result array
const result = [];

// Iterate over each year
for (const yearStr of Object.keys(inputData)) {
    const year = parseInt(yearStr);
    const yearData = inputData[yearStr];
    
    // Iterate over each month in the current year
    for (const monthStr of Object.keys(yearData)) {
        const monthName = getShortMonthName(monthStr);
        const monthData = yearData[monthStr];
        
        // Iterate over each week in the current month
        for (const weekStr of Object.keys(monthData)) {
            const weekNo = parseInt(weekStr);
            const weekTotal = monthData[weekStr].length;
            
            // Push the summary entry to the result
            result.push({
                year: year,
                month: monthName,
                week_no: weekNo,
                week_total: weekTotal
            });
        }
    }
}

// Use this result for your charting
console.log(result);

Output

Running this code will produce the following array (aligned with your input data):

[
    { year: 2011, month: 'Jan', week_no: 1, week_total: 2 },
    { year: 2011, month: 'Feb', week_no: 1, week_total: 1 },
    { year: 2011, month: 'Feb', week_no: 3, week_total: 2 },
    { year: 2012, month: 'Jan', week_no: 1, week_total: 1 },
    { year: 2012, month: 'Mar', week_no: 1, week_total: 1 }
]

Notes

  • Localization: If you need month names in a different language, change the 'en-US' parameter in Intl.DateTimeFormat to your desired locale (e.g., 'es-ES' for Spanish).
  • Dynamic Week Numbering: This solution uses the week keys directly from your input. If you need to calculate week numbers from the actual dates (like ISO week numbers), we can adjust the code to parse each date string and compute the week number using Date methods or utility libraries.

内容的提问来源于stack exchange,提问作者NewbieAngular

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:27:53