Why does linter error with "An argument for 'thisArg' was not provided"?

I'd like to use spread syntax in this code :

Math.min.apply( ...categories.map( cat => cat.category_id ) );

which shows the following lint error:

An argument for 'thisArg' was not provided

How to use spread syntax on a function that returns an array ?

3 answers

  • answered 2018-11-08 07:01 Jan Wendland

    Don't use apply.

    Math.min(...categories.map(c => c.category_id))

  • answered 2018-11-08 07:01 nrgwsth

    apply takes context and the arguments in array. So use this,

    Math.min.apply(null, categories.map( cat => cat.category_id ) ));

    But as apply doesnt use the context (see implementation here), you should simply use,

    Math.min(...categories.map( cat => cat.category_id ) ));

  • answered 2018-11-08 07:03 SkyzohKey

    Your code is incorrect. Function.apply does except a this argument first. For example:

    Math.min.apply(Math, ...categories.map(cat => cat.category_id));

    The question here is to know if you actually needs to use apply or not.

    All in all this is just a ''syntax'' error.