[英]Arrow function expected a return value
我該如何解決這個警告? 我使用的方式是不是錯了。map?
我正在嘗試顯示通過包含該信息的另一個數組接收到的每個月的總銷售額和總收入,以便稍后在圖表上顯示。
var arrEneSales = [0, 0, 0, 0, 0, 0];
var arrJulSales = [0, 0, 0, 0, 0, 0];
var arrMoneyEne = [0, 0, 0, 0, 0, 0];
var arrMoneyJul = [0, 0, 0, 0, 0, 0];
this.state.sales.map((item) => {
var date = new Date(item[0].q1_date.seconds * 1000);
if (date.getMonth() + 1 < 6) {
arrEneSales[date.getMonth() + 1] += 1;
arrMoneyEne[date.getMonth() + 1] += item[0].total;
} else {
arrJulSales[date.getMonth() + 1] += 1;
arrMoneyJul[date.getMonth() + 1] += item[0].total;
}
});
Map
用於返回數組。 如果您不想返回任何內容,只需遍歷數組,請不要使用map
像這樣使用forEach
。
this.state.sales.forEach((item) => {
var date = new Date(item[0].q1_date.seconds * 1000);
if (date.getMonth() + 1 < 6) {
arrEneSales[date.getMonth() + 1] += 1;
arrMoneyEne[date.getMonth() + 1] += item[0].total;
} else {
arrJulSales[date.getMonth() + 1] += 1;
arrMoneyJul[date.getMonth() + 1] += item[0].total;
}
});
Map 像這樣工作。
var returnedArray = this.state.sales.map((item) => {
var date = new Date(item[0].q1_date.seconds * 1000);
if (date.getMonth() + 1 < 6) {
arrEneSales[date.getMonth() + 1] += 1;
arrMoneyEne[date.getMonth() + 1] += item[0].total;
} else {
arrJulSales[date.getMonth() + 1] += 1;
arrMoneyJul[date.getMonth() + 1] += item[0].total;
}
return item;
});
從類型定義
/**
* Performs the specified action for each element in an array.
* @param callbackfn A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array.
* @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
*/
forEach(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any): void;
/**
* Calls a defined callback function on each element of an array, and returns an array that contains the results.
* @param callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.
* @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
*/
map<U>(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): U[];
所以你看到map
function 應該返回一個數組。 你應該使用forEach
什么都不返回/void
訪問MDN以獲取有關 map function 的 javascript 文檔
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.