繁体   English   中英

箭头 function 预期返回值

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM