Home > Mobile >  Return statement should not contain assignment in javascript using filter function
Return statement should not contain assignment in javascript using filter function

Time:12-29

I am using following snippet

const country = '';
const countryCodes = ['SE', 'NL', 'SE', 'FR'];
countryCodes.filter((countryCode, index) => {
  country  = countryCodes.indexOf(countryCode) === index ? `'${countryCode}'` : '';
});

What I am trying is to have following to assign the unique values to country. so country will have following values SENLFR But I got eslint error:

error Return statement should not contain assignment no-return-assign

I am aware about that filter need to meet both true and false conditions, which I think it is met in my enhanced if statement. But I don't know what is missing

CodePudding user response:

If trying to remove dups, use a set, which rejects duplicates, then spread it back into an array...

const countryCodes = ['SE', 'NL', 'SE', 'FR']
const result = [...new Set(countryCodes)]
console.log(result)

// concatenated...
console.log(result.join(''))

  • Related