简体   繁体   English

Array.prototype.map() 期望在箭头函数结束时返回一个值

[英]Array.prototype.map() expects a value to be returned at the end of arrow function

I have this array.map() arrow function and have the following error: "Array.prototype.map() expects a value to be returned at the end of arrow function array-callback-return"我有这个 array.map() 箭头函数并有以下错误:“Array.prototype.map() 期望在箭头函数数组回调返回的末尾返回一个值”

Here is my code:这是我的代码:

          {productList.map((val) => {
            const category = val.CategoryName;
            // const quantity = val.ProductQuantity
            if (category === "MotorcycleParts") {
              if (val.ProductQuantity !== 0) {
                return (
                  <div>
                    <div>
                      <div class="buyNowBtnDiv">
                        {localStorage.getItem("username") === "" ||
                        localStorage.length === 0 ? (
                          <div id="buyAddBtn">
                          </div>
                        ) : (
                          <div id="buyAddBtn">
                          </div>
                        )}
                      </div>
                    </div>
                  </div>
                );
              }
            }
          })}

I have read about the error and I see it has something to do with requiring a returning value, but I do have this?我已经阅读了有关该错误的信息,我发现它与需要返回值有关,但我确实有这个错误?

A code example with a fix would help, I think it would be something simple.带有修复的代码示例会有所帮助,我认为这很简单。

map has to return a value for each item. map必须为每个项目返回一个值。

If you want to do some filtering, you should consider using Array.prototype.filter() before, so you can remove the if s in your map.如果你想做一些过滤,你应该考虑使用Array.prototype.filter()之前,这样你就可以删除地图中的if

The code is also way more easy to read.代码也更容易阅读。

      {productList.filter(val => val.CategoryName === "MotorcycleParts" && val.ProductQuantity !== 0)
                  .map((val) => {
            return (
              <div>
                <div>
                  <div class="buyNowBtnDiv">
                    {localStorage.getItem("username") === "" ||
                    localStorage.length === 0 ? (
                      <div id="buyAddBtn">
                      </div>
                    ) : (
                      <div id="buyAddBtn">
                      </div>
                    )}
                  </div>
                </div>
              </div>
            );
      })}

You could just return null when the listed conditions are not matched:当列出的条件不匹配时,您可以只返回 null:

          {productList.map((val) => {
        const category = val.CategoryName;
        // const quantity = val.ProductQuantity
        if (category === "MotorcycleParts") {
          if (val.ProductQuantity !== 0) {
            return (
              <div>
                <div>
                  <div class="buyNowBtnDiv">
                    {localStorage.getItem("username") === "" ||
                    localStorage.length === 0 ? (
                      <div id="buyAddBtn">
                      </div>
                    ) : (
                      <div id="buyAddBtn">
                      </div>
                    )}
                  </div>
                </div>
              </div>
            );
          }
         return null;
        }
      })}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Array.prototype.map() 期望返回一个值,但不能根据其他问题返回 null,在箭头函数的末尾 - Array.prototype.map() expects a value to be returned, but can't return null as per other questions, at the end of arrow function Array.prototype.map() 期望来自箭头的返回值 function array-callback-return - Array.prototype.map() expects a return value from arrow function array-callback-return Array.prototype.map() 期望箭头函数的返回值 - Apollo 客户端 -Reactjs - Array.prototype.map() expects a return value from arrow function - Apollo client -Reactjs Array.prototype.filter() 期望在箭头函数的末尾返回一个值 - Array.prototype.filter() expects a value to be returned at the end of arrow function 如何解决这个警告警告 Array.prototype.map() 期望从箭头函数数组回调返回的返回值? - How fix this warrning warning Array.prototype.map() expects a return value from arrow function array-callback-return? Array.prototype.filter() 期望在箭头函数 array-callback-return 结束时返回一个值 - Array.prototype.filter() expects a value to be returned at the end of arrow function array-callback-return react js Array.prototype.filter() 期望在箭头结束时返回一个值 function - react js Array.prototype.filter() expects a value to be returned at the end of arrow function 我返回 `boolean` 但 Array.prototype.filter() 期望在箭头 function 的末尾返回一个值 - i return `boolean` but Array.prototype.filter() expects a value to be returned at the end of arrow function 将array.prototype.map与平均功能一起使用 - Using array.prototype.map with average function Array.prototype.map()之后的下一个函数可以安全地假定map中的所有回调都已返回吗? - Can the next function after Array.prototype.map() safely assume all callback in map have returned?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM