我正在嘗試創建一個物件,其“鍵”將是財政年度(FY),“值”是季度日期。
//Format
{
FY:{
[Quarter dates]
},
}
季度日期計數從 1 到 4 不等。如果只給出四分之一的資訊,則以上一個 FY 為參考,并增加 1 以了解當前 FY。
根據期間結束日期,我可以對季度進行分組,但計算財政年度是我需要幫助的。FY 將遵循這些規則。
重復次數最多的季度將是財政年度。
如果季度有兩個不同的年份,每個年份計數為 2,那么我們可以選擇最小的年份作為 FY。例如:期間 =[06/21],季度 =[09/20,12/20,03/21,06/21] 然后 FY =2020
公司可以更改其期間結束日期,因此您可以預期季度日期之間存在差距。在以下示例中,公司使用 12 月作為截至 2019 年的期末,之后他們使用 3 月作為期末。省略了四分之一(1 月、2 月、3 月)的差距。
const quarters = [
"12 / 30 / 2021",
"09 / 30 / 2021",
"06 / 30 / 2021",
"03 / 30 / 2021",
"12 / 30 / 2020",
"09 / 30 / 2020",
"06 / 30 / 2020",
"12 / 30 / 2019",
"09 / 30 / 2019",
"06 / 30 / 2019",
"03 / 30 / 2019",
"12 / 30 / 2018",
"09 / 30 / 2018",
"06 / 30 / 2018",
"03 / 30 / 2018",
];
const periods = [
"03 / 30 / 2021",
"12 / 30 / 2019",
"12 / 30 / 2018",
"12/30/2017"
];
const groupQuarters = [];
let group = [];
while (periods.length > 0) {
let p = new Date(periods[0].toString());
group = [];
while (quarters.length > 0) {
let q = new Date(quarters[0].toString());
if (q > p) {
group.push(quarters[0]);
quarters.shift();
} else {
break;
}
}
const yearCal = (groups) => {
// unable to figure this out.
return 2020;
}
groupQuarters.push({ [yearCal(group)]: group });
periods.shift();
}
console.log(groupQuarters);
Expected output:
[
{
2021: [
'12 / 30 / 2021',
'09 / 30 / 2021',
'06 / 30 / 2021'
]
},
{
2020: [
'03 / 30 / 2021',
'12 / 30 / 2020',
'09 / 30 / 2020',
'06 / 30 / 2020'
]
},
{
2019: [
'12 / 30 / 2019',
'09 / 30 / 2019',
'06 / 30 / 2019',
'03 / 30 / 2019'
]
},
{
2018: [
'12 / 30 / 2018',
'09 / 30 / 2018',
'06 / 30 / 2018',
'03 / 30 / 2018'
]
}
]
Test Case - 2:
const quarters = [
"12 / 30 / 2020",
"09 / 30 / 2020",
"06 / 30 / 2020",
"03 / 30 / 2020",
"12 / 30 / 2019",
"09 / 30 / 2019",
"06 / 30 / 2019",
"03 / 30 / 2019",
"12 / 30 / 2018"
];
const periods = [
"09 / 30 / 2020",
"09 / 30 / 2019",
"09 / 30 / 2018",
];
Expected Output:
[
{
2021: [
'12 / 30 / 2020',
]
},
{
2020: [
"09 / 30 / 2020",
"06 / 30 / 2020",
"03 / 30 / 2020",
"12 / 30 / 2019",
]
},
{
2019: [
"09 / 30 / 2019",
"06 / 30 / 2019",
"03 / 30 / 2019",
"12 / 30 / 2018",
]
},
]
uj5u.com熱心網友回復:
假設一個時期內沒有差距——即屬于同一時期的兩個季度之間沒有差距——你可以應用這個邏輯:
- 當最后一個季度已知時(即等于期末),減去 2 個季度并將該季度的年份作為財政年度
- 如果不知道最后一個季度的周期——這可能發生在陣列的開頭,當前周期尚未完成——在該周期的第一季度添加一個季度,并將該季度的年份作為財政年度.
由于最好將陣列一對一映射periods以獲得結果陣列,因此有時可能需要去除最后一個句點條目(如在測驗用例 1 中),或者在該陣列的開頭插入一個未完成的句點(也在測驗用例 1 中)。
不相關,但我避免了此任務的 Date 物件,并包含將輸入字串轉換為數字并回傳的函式:
function chunkByYear(quarters, periods) {
// Converter functions between string and unique quarter number
const toQuarter = s => s.match(/^\d |\d $/g).reduce((m, y) => m/3 - 1 y*4);
const toDate = q => ((q % 4 1)*3 "/30/" (q >> 2)).padStart(10, "0");
const periodNums = periods.map(toQuarter);
const quarterNums = quarters.map(toQuarter);
// Make sure all quarters are after at least one period end
while (quarterNums[quarterNums.length-1] <= periodNums[periodNums.length-1]) periodNums.push(periodNums[periodNums.length-1] - 4);
// Remove periods that have no content:
while (quarterNums[0] <= periodNums[0]) periodNums.shift();
quarterNums.push(0); // To make sure `findIndex` always finds an index
return periodNums.map((p, i, {length}) => {
const period = quarterNums.splice(0, quarterNums.findIndex(q => q <= p));
return { [(i ? period[0] - 2 : period[period.length-1] 1) >> 2]: period.map(toDate) };
});
}
{ // test 1
const quarters = [
"12 / 30 / 2021", "09 / 30 / 2021", "06 / 30 / 2021",
"03 / 30 / 2021", "12 / 30 / 2020", "09 / 30 / 2020", "06 / 30 / 2020",
"12 / 30 / 2019", "09 / 30 / 2019", "06 / 30 / 2019", "03 / 30 / 2019",
"12 / 30 / 2018", "09 / 30 / 2018", "06 / 30 / 2018", "03 / 30 / 2018",
];
const periods = ["03 / 30 / 2021", "12 / 30 / 2019", "12 / 30 / 2018", "12 / 30 / 2017"];
console.log(chunkByYear(quarters, periods));
}
{ // test 2
const quarters = [
"12 / 30 / 2020",
"09 / 30 / 2020", "06 / 30 / 2020", "03 / 30 / 2020", "12 / 30 / 2019",
"09 / 30 / 2019", "06 / 30 / 2019", "03 / 30 / 2019", "12 / 30 / 2018"
];
const periods = ["09 / 30 / 2020", "09 / 30 / 2019", "09 / 30 / 2018"];
console.log(chunkByYear(quarters, periods));
}
{ // test 3
const quarters = [
"06 / 30 / 2021", "03 / 30 / 2021", "12 / 30 / 2020", "09 / 30 / 2020",
"06 / 30 / 2020", "03 / 30 / 2020", "12 / 30 / 2019", "09 / 30 / 2019",
"06 / 30 / 2019",
];
const periods = ["06 / 30 / 2021", "06 / 30 / 2020", "06 / 30 / 2019"];
console.log(chunkByYear(quarters, periods));
}
{ // test 4
const quarters = [
"06 / 30 / 2021", "03 / 30 / 2021", "12 / 30 / 2020", "09 / 30 / 2020",
"06 / 31 / 2020", "03 / 30 / 2020", "12 / 30 / 2019", "09 / 30 / 2019",
"06 / 30 / 2019", "03 / 30 / 2019", "12 / 30 / 2018", "09 / 30 / 2018",
];
const periods = ["06 / 30 / 2021", "06 / 31 / 2020", "06 / 30 / 2019"];
console.log(chunkByYear(quarters, periods));
}
uj5u.com熱心網友回復:
你可以試試 :
const quarters = [
"12 / 30 / 2021",
"09 / 30 / 2021",
"06 / 30 / 2021",
"03 / 30 / 2021",
"12 / 30 / 2020",
"09 / 30 / 2020",
"06 / 30 / 2020",
"12 / 30 / 2019",
"09 / 30 / 2019",
"06 / 30 / 2019",
"03 / 30 / 2019",
"12 / 30 / 2018",
"09 / 30 / 2018",
"06 / 30 / 2018",
"03 / 30 / 2018",
]
const periods = [
"03 / 30 / 2021",
"12 / 30 / 2019",
"12 / 30 / 2018",
"12/30/2017"
]
const quarters1 = [
"12 / 30 / 2020",
"09 / 30 / 2020",
"06 / 30 / 2020",
"03 / 30 / 2020",
"12 / 30 / 2019",
"09 / 30 / 2019",
"06 / 30 / 2019",
"03 / 30 / 2019",
"12 / 30 / 2018"
];
const periods1 = [
"09 / 30 / 2020",
"09 / 30 / 2019",
"09 / 30 / 2018",
];
const getGroupQuarter = (qt, pr) => {
let tmpCheck = qt
return pr.reduce((result,period) => {
let datePeriod = new Date(period)
let key = getKeyQuarter(datePeriod)
let qtrFill = tmpCheck.filter(quarter => new Date(quarter) > datePeriod)
tmpCheck = tmpCheck.filter(quarter =>!(new Date(quarter) > datePeriod))
return !qtrFill.length ? result :[...result, {[key] : qtrFill}]
}, [])
}
const getKeyQuarter = (pr) => {
let monthValidate = pr.getMonth()
return monthValidate > 5 ? pr.getFullYear() 1 : pr.getFullYear()
}
console.log(getGroupQuarter(quarters1, periods1));
console.log(getGroupQuarter(quarters, periods));
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/486316.html
標籤:javascript 数组 算法 javascript 对象
