我有幾年的財務資料:
'月-年',銷售額例如:
['jan-2020',984655],['feb-2020',-21632],等等......
- 為了利潤和 - 當虧損時。我想計算以下內容:
- 資料中的月數
- 整個期間的虧損/盈利金額
- 最賺錢的月份(日期和金額)
- 損失最差的月份(日期和金額)平均變化(總/月數)
我至少嘗試了以下方法以獲得幾個月但不成功`
function getMonthDifference(startDate, endDate) {
return(
endDate.getMonth() -
startDate.getMonth()
);
}
console.log(getMonthDifference)
uj5u.com熱心網友回復:
這是你想看到的嗎?我誤解了你的問題嗎?
tempData = [['jan-2020',984655],['feb-2020',-21632]]
numbers = tempData.length
loss = tempData.filter(o => o[1] < 0 ).reduce( (o1,o2) => o1[1] o2[1]);
profit = tempData.filter(o => o[1] > 0 ).reduce( (o1,o2) => o1[1] o2[1]);
profitable = tempData.sort((o1, o2) => o1[1] > o2[1] ? -1 : (o1[1] < o2[1] ? 1 : 0))[0]
losses = tempData.sort((o1, o2) => o1[1] > o2[1] ? 1 : (o1[1] < o2[1] ? -1 : 0))[0]
avg = tempData.reduce( (o1,o2) => o1[1] o2[1])/numbers;
uj5u.com熱心網友回復:
非常感謝您的回復。想要看到的是:總月數:x 總計:$xxxx 平均變化:$-xxx 最大利潤增長:2020 年 1 月(984655 美元)利潤最大下降:2022 年 2 月($-218332)
uj5u.com熱心網友回復:
您可以使用常規的 js 函式來實作這一點。閱讀行內注釋,檢查結果:
// Data
const data = [
['jan-2020', 984655],
['feb-2020', -21632],
['mar-2020', 621632],
['apr-2020', 283726],
['may-2020', 10039],
['jun-2020', -40044],
['jul-2020', -10032],
['aug-2020', 50038],
['sep-2020', 915633],
['oct-2020', 494372],
['nov-2020', 374757],
['dec-2020', -51611]
];
// Get number of month in data
console.log("Total month in data:", data.length);
console.log("---------");
// Amount of loss over entire period
const lossMonths = data.filter(el => el[1] < 0);
console.log("All loss months:");
for(const mo of lossMonths) console.log(`${mo[0]}: ${mo[1]}`);
console.log("Sum of losses:", lossMonths.map(el => el[1]).reduce((a, b) => a b));
console.log("---------");
// Amount of profit over entire period
const profitMonths = data.filter(el => el[1] > 0);
console.log("All profit months:");
for(const mo of profitMonths) console.log(`${mo[0]}: ${mo[1]}`);
console.log("Sum of profit:", profitMonths.map(el => el[1]).reduce((a, b) => a b));
console.log("---------");
// Most worst month (date and amount)
const lossMonthSorted = lossMonths.sort((a, b) => a[1] > b[1]);
console.log("Most worst month:", lossMonthSorted[0].join(": "));
console.log("---------");
// Most profitable month (date and amount)
const profitMonthsSorted = profitMonths.sort((a, b) => a[1] < b[1]);
console.log("Most profitable month:", profitMonthsSorted[0].join(": "));
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/530997.html
標籤:javascript数组
上一篇:原始錯誤:錯誤的應用程式:應用程式路徑需要是絕對路徑或壓縮應用程式檔案的URL:
下一篇:計算字典串列中的出現次數
