簡體   English   中英

如何解決此“預期在箭頭函數中返回值”錯誤

[英]How to fix this "expected to return a value in arrow function" error

我想解決這個問題

我嘗試了每件事:p


    Tabs.map(tab => {
     Lessons.filter(lesson => lesson.section_id === undefined && lesson.tab_id === tab.id).map(lesson => {
      this.state.items.push(
       {
        id: lesson.id,
        slug: lesson.slug
       }
      );
     });
     Sections.filter(section => section.tab_id === tab.id).map(section => {
      Lessons.filter(lesson => lesson.section_id === section.id).map(lesson => {
       this.state.items.push(
        {
         id: lesson.id,
         slug: lesson.slug
        }
       );
      });
     });
    });

第 31 行:預期在箭頭函數 array-callback-return 中返回一個值
第 32 行:預期在箭頭函數 array-callback-return 中返回一個值
第 40 行:預期在箭頭函數 array-callback-return 中返回一個值
第 41 行:預期在箭頭函數 array-callback-return 中返回一個值

搜索關鍵字以了解有關每個警告的更多信息。 要忽略,請將 // eslint-disable-next-line 添加到前一行。

如果您嘗試在功能上遍歷數組,請使用.forEach而不是.map

當您想從現有數組的值創建新數組時,應該使用.map

有關更多信息,請參閱此答案: https : //stackoverflow.com/a/34426481/1337057

 Tabs.forEach(tab => {
 Lessons.filter(lesson => lesson.section_id === undefined && lesson.tab_id === tab.id).forEach(lesson => {
  this.state.items.push(
   {
    id: lesson.id,
    slug: lesson.slug
   }
  );
 });
 Sections.filter(section => section.tab_id === tab.id).forEach(section => {
  Lessons.filter(lesson => lesson.section_id === section.id).forEach(lesson => {
   this.state.items.push(
    {
     id: lesson.id,
     slug: lesson.slug
    }
   );
  });
 });
});

只需將您的地圖替換為 forEach。 Map 應該返回一些東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM