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
- Traverse the Nested Structure: Iterate through each level of your input object (year → month → week) to extract key values.
- Convert Month Numbers to Short Names: Use the built-in
Intl.DateTimeFormatto reliably generate abbreviated month names (like "Jan" for "01") without hardcoding. - 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.
- 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 inIntl.DateTimeFormatto 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
datestring and compute the week number usingDatemethods or utility libraries.
内容的提问来源于stack exchange,提问作者NewbieAngular
相关产品推荐
相关产品推荐

