简体   繁体   中英

Using a .map below how do I check that everything is truthy and only required items are outputted

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?

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 === 'document') {
          return {
            url: `/${resource.url}`,
            changefreq: 'weekly',
            priority: 0.6,
          }
        }
      })
return [
        {
          url: '/',
          changefreq: 'daily',
          priority: 1,
        },
        {
          url: '/account',
          changefreq: 'daily',
          priority: 1,
        },
        ...urlModule,
      ]
    },

Array.map() function requires to return something for each element of the original array so that's why you're asked to add an else, to cover every possible case.

If you want to return something only for certain elements of the original array you can use a for of like this

 const resources = [{ resourceType: 'brand', url: 'brandUrl' }, { resourceType: 'product', url: 'productUrl' }, { resourceType: 'category', url: 'categoryUrl' }, { resourceType: 'document', url: 'documentUrl' }]; let urlModule = []; for (const resource of resources) { if (resource.resourceType === 'brand') { urlModule.push({ url: `/${resource.url}`, changefreq: 'weekly', priority: 0.9, }) } else if (resource.resourceType === 'product') { urlModule.push({ url: `/${resource.url}`, changefreq: 'monthly', priority: 0.8, }) } else if (resource.resourceType === 'document') { urlModule.push({ url: `/${resource.url}`, changefreq: 'weekly', priority: 0.6, }) } } console.log(urlModule);

Hope that can help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM