[英]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.