unicorn/no-array-method-this-argument

Disallow the thisArg argument of array methods such as map and filter

map, filter, and similar methods take a second argument that becomes this inside the callback. It comes after the callback, where it's easy to miss. With an arrow function it does nothing, because an arrow function keeps the this from the surrounding code.

The rule checks every, filter, find, findLast, findIndex, findLastIndex, flatMap, forEach, map, and some. It also checks the third argument of Array.from() and Array.fromAsync().

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"unicorn/no-array-method-this-argument" = "error" # or "warn"

Examples#

example.js1 problem
const mine = items.filter(function (item) {
  return item.owner === this.id;
}, user);
example.js1 problem
const rows = items.map(table.renderRow, table);
example.js1 problem
const cards = Array.from(nodes, function (node) {
  return this.wrap(node);
}, view);
example.jsNo problems
const mine = items.filter((item) => item.owner === user.id);
const rows = items.map((item) => table.renderRow(item));
const cards = Array.from(nodes, (node) => view.wrap(node));

The rule skips a call if its first argument can't be a function. It also skips library methods with the same names, such as _.map(), R.map(), $.map(), and React.Children.map(), because their second argument means something else:

example.jsNo problems
const match = collection.find("email", address);
const names = _.map(users, formatName);