简体   繁体   English

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

I am using the map function with an if statement that doesn't end in else.我正在使用带有不以 else 结尾的 if 语句的 map 函数。 I have to be strict that only the defined recourseTypes are shown.我必须严格只显示定义的 recourseTypes。

in eslint I get an error on my => as it doesn't end in an else在 eslint 中,我的 => 出现错误,因为它没有以 else 结尾

Array.prototype.map() expects a value to be returned at the end of arrow function. Array.prototype.map() 期望在箭头函数结束时返回一个值。

Is there a correct way to write this?有没有正确的方法来写这个?

Code:代码:

   routes: async () => {
      const apiURL = process.env.BASE_URL
      const response = await axios.get(`${apiURL}/urls`)
      const { data: resources } = response.data
      const urlModule = resources.map((resource) => {
        const { resourceType } = resource

    if (resourceType === 'brand') {
      return {
        url: `/${resource.url}`,
        changefreq: 'weekly',
        priority: 0.9,
      }
    } else if (resourceType === 'product') {
      return {
        url: `/${resource.url}`,
        changefreq: 'monthly',
        priority: 0.8,
      }
    } else if (resourceType === 'category') {
      return {
        url: `/${resource.url}`,
        changefreq: 'weekly',
        priority: 0.7,
      }
    } else if (resourceType === 'document') {
      return {
        url: `/${resource.url}`,
        changefreq: 'weekly',
        priority: 0.6,
      }
    }
  })

  return [
    {
      url: '/',
      changefreq: 'daily',
      priority: 1,
    },
    {
      url: '/account',
      changefreq: 'daily',
      priority: 1,
    },
    {
      url: '/account/order-history',
      changefreq: 'daily',
      priority: 1,
    },
    ...urlModule,
  ]
},

You need to figure out a default to return in the else case.您需要找出在else情况下返回的默认值。 It doesn't technically need to be in an else block since you return in the other if blocks, it can just be at the end of the function.从技术上讲,它不需要在else块中,因为您在另一个if块中返回,它可以只是在函数的末尾。 To ensure that only the allowed resourceType s are included, you can return some sort of output that you'll be able to recognize as an "error" if someone isn't using the right resourceType .为确保仅包含允许的resourceType ,您可以返回某种输出,如果有人没有使用正确的resourceType ,您将能够将其识别为“错误”。 You say you don't want to return null , presumably because you don't want that in the final array, so you could just return null and then filter with a simple .filter(item => item) to make sure everything is truthy, or you could explicitly check for null .您说您不想返回null ,大概是因为您不希望在最终数组中使用它,因此您可以返回null然后使用简单的.filter(item => item)进行过滤以确保一切都是真实的,或者您可以显式检查null Either way, you're going to have to return some sort of recognizable "error" default and handle that case.无论哪种方式,您都必须返回某种可识别的“错误”默认值并处理这种情况。

Clarification : resources array contains all the resourceType that we are comparing using if/else clause ?澄清: resources数组包含我们使用if/else子句比较的所有资源类型? As Array.map() will return the same number of elements as input array contains otherwise it will return with undefined.由于Array.map()将返回与输入数组包含的相同数量的元素,否则它将返回未定义的元素。

Suggestion : Instead of multiple if/else-if clause, we can use multiple if statements for a better performance.建议:我们可以使用多个if语句来代替多个if/else-if子句以获得更好的性能。

Demo :演示

 // Response coming from API. const resources = [{ resourceType: 'brand', url: 'brandUrl' }, { resourceType: 'product', url: 'productUrl' }, { resourceType: 'category', url: 'categoryUrl' }, { resourceType: 'document', url: 'documentUrl' }]; // Array of required ResourceTypes. const requiredResourceTypes = ['brand', 'product', 'document']; // Logic to filter and get the URL's object of the required resourceTypes. const urlModule = resources.filter(({resourceType}) => requiredResourceTypes.includes(resourceType)).map((resource) => { const { resourceType } = resource if (resourceType === 'brand') { return { url: `/${resource.url}`, changefreq: 'weekly', priority: 0.9, } } if (resourceType === 'product') { return { url: `/${resource.url}`, changefreq: 'monthly', priority: 0.8, } } if (resourceType === 'category') { return { url: `/${resource.url}`, changefreq: 'weekly', priority: 0.7, } } if (resourceType === 'document') { return { url: `/${resource.url}`, changefreq: 'weekly', priority: 0.6, } } }); console.log(urlModule);

Performance test result screenshot :性能测试结果截图

在此处输入图像描述

暂无
暂无

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

相关问题 Array.prototype.map() 期望在箭头函数结束时返回一个值 - Array.prototype.map() expects a value to be returned 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.map() 期望从箭头函数数组回调返回的返回值? - How fix this warrning warning Array.prototype.map() expects a return value from arrow function array-callback-return? Array.prototype.filter() 期望在箭头函数的末尾返回一个值 - Array.prototype.filter() expects a value to be returned at the end of arrow function Array.prototype.filter() 期望在箭头函数 array-callback-return 结束时返回一个值 - Array.prototype.filter() expects a value to be returned at the end of arrow function array-callback-return 我返回 `boolean` 但 Array.prototype.filter() 期望在箭头 function 的末尾返回一个值 - i return `boolean` but Array.prototype.filter() expects a value to be returned at the end of arrow function react js Array.prototype.filter() 期望在箭头结束时返回一个值 function - react js Array.prototype.filter() expects a value to be returned at the end of arrow function Array.prototype.filter() 期望在函数 array-callback-return 结束时返回一个值 - Array.prototype.filter() expects a value to be returned at the end of function array-callback-return 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