array.prototype.map()期望返回一个值,但是在箭头函数结束时,可以根据其他问题返回null

发布于 2025-02-09 04:23:23 字数 1413 浏览 1 评论 0原文

我正在使用地图函数,其中if语句不会结束。我必须严格认为仅显示定义的recoursetypes。

在eslint中,我在我的=&gt上遇到了一个错误;因为它不会以其他方式结束

array.prototype.map()期望在箭头函数末尾返回一个值。

有正确的写作方法吗?

代码:

   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,
  ]
},

I am using the map function with an if statement that doesn't end in else. I have to be strict that only the defined recourseTypes are shown.

in eslint I get an error on my => as it doesn't end in an else

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

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,
  ]
},

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傲性难收 2025-02-16 04:23:23

澄清: 资源数组包含我们使用If/else子句比较的所有资源类型?因为array.map()将返回与输入数组包含的相同数量的元素数量,否则它将以未定义的方式返回。

建议: 而不是多个如果/else-if子句,我们可以使用多个如果语句以获得更好的性能。

演示

// 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);

性能测试结果屏幕截图

Clarification : resources array contains all the resourceType that we are comparing using if/else clause ? As Array.map() will return the same number of elements as input array contains otherwise it will return with undefined.

Suggestion : Instead of multiple if/else-if clause, we can use multiple if statements for a better performance.

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 :

enter image description here

伤痕我心 2025-02-16 04:23:23

您需要找出一个默认值才能返回else案例。从技术上讲,它不需要在else块中,因为您在其他 block中返回 block,它只能在函数的末尾。为了确保仅包括允许的ResourceType s,如果某人不使用正确的ResourceType,您可以返回某种输出,您可以识别为“错误”。您说您不想返回null,大概是因为您不希望在最终数组中使用它,因此您只需返回null,然后使用简单的过滤.filter(item => item)确保一切都是真实的,或者您可以明确检查null。无论哪种方式,您都必须返回某种可识别的“错误”默认值并处理该情况。

You need to figure out a default to return in the else case. 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. To ensure that only the allowed resourceTypes 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. 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. Either way, you're going to have to return some sort of recognizable "error" default and handle that case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文