1
Object.keys(updateValues).forEach(field => {
   allowedFields.indexOf(field) === -1 && delete updateValues[field];
});

In the above code I am iterating over updateValues and deleting indexes that no exist in allowedFields, however after enabling Eslint the delete updateValues[field]; is failing the no-param-reassign rule.

I was thinking of using a method which destructures the [field] and returns the remaining. Can anyone provide an example of this or offer and direction?

James
  • 205
  • 1
  • 8
  • 2
    "*I was thinking of using a method which destructures the [field] and returns the remaining.*" not a good approach IMO. You don't need to really loop and create many objects each with one less key. Just extract all keys -> remove the unneeded ones -> get an object with the rest of the keys. – VLAZ Dec 02 '20 at 10:06
  • I did think it was overthinking the problem, would you provide an example of your solution? I cannot find a way of removing that does not end up with Eslint complaining – James Dec 02 '20 at 10:10
  • See [Filter object properties by key in ES6](https://stackoverflow.com/q/38750705) in particular [this answer](https://stackoverflow.com/a/56081419/) does exactly the same operation where you whitelist some keys and create an object from them. Multiple other solutions there including blacklisting or direct re-mapping of an object to only the expected keys. See also [JavaScript: filter() for Objects](https://stackoverflow.com/q/5072136) – VLAZ Dec 02 '20 at 10:21
  • ah yes, I understand. Thanks pal – James Dec 02 '20 at 10:33

0 Answers0