array.prototype.map()期望返回一个值,但是在箭头函数结束时,可以根据其他问题返回null
我正在使用地图函数,其中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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
澄清:
资源
数组包含我们使用If/else
子句比较的所有资源类型?因为array.map()
将返回与输入数组包含的相同数量的元素数量,否则它将以未定义的方式返回。建议: 而不是多个
如果/else-if
子句,我们可以使用多个如果
语句以获得更好的性能。演示:
性能测试结果屏幕截图:
Clarification :
resources
array contains all the resourceType that we are comparing usingif/else
clause ? AsArray.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 multipleif
statements for a better performance.Demo :
Performance test result screenshot :
您需要找出一个默认值才能返回
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 anelse
block since you return in the otherif
blocks, it can just be at the end of the function. To ensure that only the allowedresourceType
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 rightresourceType
. You say you don't want to returnnull
, presumably because you don't want that in the final array, so you could just returnnull
and then filter with a simple.filter(item => item)
to make sure everything is truthy, or you could explicitly check fornull
. Either way, you're going to have to return some sort of recognizable "error" default and handle that case.