javascripteslintairbnb-js-styleguide

unexpected use of comma using map on a object


I'm using the AirBnb style guide and get an "unexpected use of comma" error here at the end of the first line of code:

  myApp.fields.map(field => (field.obj.label = field.label.default,
    field.label.textContent = field.obj.label));

I can rewrite it to this to remove the error:

  myApp.fields.map(field => field.obj.label = field.label.default);
  myApp.fields.map(field => field.label.textContent = field.obj.label);

https://eslint.org/docs/rules/no-sequences

The way I see the first bit of code that the map loop only runs once of the fields where the second one runs twice.

I can confirm that both parts of the map above are executing and not just the last one. Is there something else I'm missing?


Solution

  • Since you're not transforming the array, you could just use forEach() instead of map(). If you insist on making it into a one-liner, and don't want to violate the no-sequences rule:

    myApp.fields.forEach(field => {field.obj.label = field.label.default; field.label.textContent = field.obj.label});
    

    More readable:

    myApp.fields.forEach((field) => {
        field.obj.label = field.label.default;
        field.label.textContent = field.obj.label;
    });