我有以下陣列
[
{ Month: '2021-05', Count: 36 },
{ Month: '2021-06', Count: 1048 },
{ Month: '2021-07', Count: 572 },
{ Month: '2021-09', Count: 3 },
{ Month: '2021-12', Count: 52 },
{ Month: '2022-01', Count: 4 },
{ Month: '2022-02', Count: 273 },
{ Month: '2022-04', Count: 96 }
]
我錯過了幾個月的地方。我知道需要多少個月(可能是 12 或更多或更少),我需要將缺失的月份(如本例中的 2021-08)添加為 0。如何去做?
uj5u.com熱心網友回復:
這是一種純粹的函式式方法,它將創建一個包含新專案的新陣列,并按順序插入所有缺失的月份。該代碼包含一些解釋該程序的注釋:
const parseDate = str => str.split('-').map(Number);
const formatDate = (year, month) => `${year}-${String(month).padStart(2, '0')}`;
function createContinuousMonthCounts (array) {
const all = [];
// get initial year/month values from first item
let [year, month] = parseDate(array[0].Month);
const advanceDate = () => {
month = 1;
if (month > 12) {
year = 1;
month = 1;
}
};
for (const item of array) {
const [y, m] = parseDate(item.Month);
// while the current month is not equal to the current item's month,
// create an entry for the month, append it, and advance to the next month
while (year !== y || month !== m) {
all.push({Month: formatDate(year, month), Count: 0});
advanceDate();
}
// after we're up to date, add the current item and advance the date
all.push({...item});
advanceDate();
}
return all;
}
const array = [
{ Month: '2021-05', Count: 36 },
{ Month: '2021-06', Count: 1048 },
{ Month: '2021-07', Count: 572 },
{ Month: '2021-09', Count: 3 },
{ Month: '2021-12', Count: 52 },
{ Month: '2022-01', Count: 4 },
{ Month: '2022-02', Count: 273 },
{ Month: '2022-04', Count: 96 },
];
const all = createContinuousMonthCounts(array);
for (const {Month, Count} of all) console.log(Month, Count);
uj5u.com熱心網友回復:
只是進入黑暗(請考慮在您的問題中添加一些代碼):
const months = [
{ Month: '2021-05', Count: 36 },
{ Month: '2021-06', Count: 1048 },
{ Month: '2021-07', Count: 572 },
{ Month: '2021-09', Count: 3 },
{ Month: '2021-12', Count: 52 },
{ Month: '2022-01', Count: 4 },
{ Month: '2022-02', Count: 273 },
{ Month: '2022-04', Count: 96 }
];
const neededMonths = [
"2021-01","2021-02","2021-03","2021-04","2021-05","2021-06","2021-07","2021-08","2021-09","2021-10","2021-11","2021-12"
]
const missedMonths = [];
months.map( m => {
if(neededMonths.indexOf(m.Month) == -1 ){
missedMonths.push(m.Month);
}
});
console.log(missedMonths);
uj5u.com熱心網友回復:
您首先需要一種方法來查找范圍之間的所有月份,然后遍歷所有月份并添加缺少的月份count: 0:
const months = [
{ Month: '2021-05', Count: 36 },
{ Month: '2021-06', Count: 1048 },
{ Month: '2021-07', Count: 572 },
{ Month: '2021-09', Count: 3 },
{ Month: '2021-12', Count: 52 },
{ Month: '2022-01', Count: 4 },
{ Month: '2022-02', Count: 273 },
{ Month: '2022-04', Count: 96 }
]
const firstMonth = months.at(0).Month;
const lastMonth = months.at(-1).Month;
const [initialYear, initialMonth] = firstMonth.split('-');
const [endingYear, endingMonth] = lastMonth.split('-');
const allMonths = [];
let currentMonth = initialMonth;
let currentYear = initialYear;
while (`${currentYear}-${('' currentMonth).padStart(2, '0')}` !== lastMonth) {
allMonths.push(`${currentYear}-${('' currentMonth).padStart(2, '0')}`);
currentMonth ;
if (currentMonth === 13) {
currentMonth = 1;
currentYear ;
}
}
allMonths.forEach(month => {
if (!months.find(m => m.Month === month)) {
months.push({Month: month, count: 0});
}
});
console.log(months);
uj5u.com熱心網友回復:
這是一個簡單的解決方案:
- 按日期對輸入陣列進行排序并迭代
- 將每個專案插入一個名為 result 的新陣列中
- 但在插入之前,檢查結果陣列中的上個月和當前月份是否有差距;使用嵌套回圈來填補空白
const array = [
{ Month: "2021-05", Count: 36 },
{ Month: "2021-06", Count: 1048 },
{ Month: "2021-07", Count: 572 },
{ Month: "2021-09", Count: 3 },
{ Month: "2021-12", Count: 52 },
{ Month: "2022-01", Count: 4 },
{ Month: "2022-02", Count: 273 },
{ Month: "2022-04", Count: 96 }
];
let result = [];
array.sort(function(a, b) {
return a.Month.localeCompare(b.Month);
});
array.forEach(function(item) {
function addmonth(yymm) {
let yy = Number(yymm.split("-")[0]);
let mm = Number(yymm.split("-")[1]) 1;
mm = 1;
if (mm > 12) {
mm = 1;
yy = 1;
}
return yy.toString() "-" mm.toString().padStart(2, "0");
}
if (result.length > 0) {
for (let yymm = addmonth(result[result.length - 1].Month); yymm < item.Month; yymm = addmonth(yymm)) {
result.push({
Month: yymm,
Count: 0
});
}
}
result.push(item);
});
console.log(result);
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/476267.html
標籤:javascript 数组
下一篇:與Typescript反應:“DetailedHTMLProps<HTMLAttributes<HTMLDivElement>,HTMLDivElement>”型別上不存在屬性
