当在 Array.prototype.map() 方法中使用箭头函数时,箭头函数中必须包含一个 return 语句来返回一个值。如果箭头函数不包含 return 语句,则会导致警告消息 'Array.prototype.map() expects a return value from arrow function array-callback-return”。以下是一个示例代码,使用返回语句修复警告:
const myArray = [1, 2, 3];
const doubledArray = myArray.map((item) => {
return item * 2;
});
console.log(doubledArray); // 输出 [2, 4, 6]
在上面的示例中,我们在箭头函数中添加了一个 return 语句,以便返回一个新的数组,其中每个元素都是原始数组的两倍。这样就不会收到警告消息了。