admin管理员组

文章数量:1305151

I am trying to sort array of object with custom function.
So, I define my function

   sortingAlgo = (a, b) => {
        // we need to use custom sorting algorithm to meet our requirements.
        a.pos = a.pos.toUpperCase();
        b.pos = b.pos.toUpperCase();
        if(a.pos.slice(0, 1) === b.pos.slice(0, 1)) {
          if (parseInt(a.pos.slice(1)) < parseInt(b.pos.slice(1))){
            return -1;
          }
          if (parseInt(a.pos.slice(1)) > parseInt(b.pos.slice(1))){
            return 1;
          }
          return 0;
        } else {
          if (parseInt(a.pos.slice(0, 1)) < parseInt(b.pos.slice(0, 1))){
            return -1;
          }
          if (parseInt(a.pos.slice(0, 1)) > parseInt(b.pos.slice(0, 1))){
            return 1;
          }
          return 0;

        }
      }

And i call it like this.

const sortedProducts = data.data.sort(this.sortingAlgo);

Now i Got Eslint error saying.

**

  • 169:5 error Assignment to property of function parameter 'a' no-param-reassign
  • 170:5 error Assignment to property of function parameter 'b' no-param-reassign

**

I searched and found that this rule is set byt airBnB. So i dont think its right to disable this rule.
So can somebody tell me how to resolve it or if disabling this rule is okay.

I am trying to sort array of object with custom function.
So, I define my function

   sortingAlgo = (a, b) => {
        // we need to use custom sorting algorithm to meet our requirements.
        a.pos = a.pos.toUpperCase();
        b.pos = b.pos.toUpperCase();
        if(a.pos.slice(0, 1) === b.pos.slice(0, 1)) {
          if (parseInt(a.pos.slice(1)) < parseInt(b.pos.slice(1))){
            return -1;
          }
          if (parseInt(a.pos.slice(1)) > parseInt(b.pos.slice(1))){
            return 1;
          }
          return 0;
        } else {
          if (parseInt(a.pos.slice(0, 1)) < parseInt(b.pos.slice(0, 1))){
            return -1;
          }
          if (parseInt(a.pos.slice(0, 1)) > parseInt(b.pos.slice(0, 1))){
            return 1;
          }
          return 0;

        }
      }

And i call it like this.

const sortedProducts = data.data.sort(this.sortingAlgo);

Now i Got Eslint error saying.

**

  • 169:5 error Assignment to property of function parameter 'a' no-param-reassign
  • 170:5 error Assignment to property of function parameter 'b' no-param-reassign

**

I searched and found that this rule is set byt airBnB. So i dont think its right to disable this rule.
So can somebody tell me how to resolve it or if disabling this rule is okay.

Share Improve this question asked Feb 24, 2019 at 5:52 Rajan LagahRajan Lagah 2,5281 gold badge26 silver badges46 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

There's no need to reassign the pos property of the function arguments (a, b). You should just assign a new variable (aPos, bPos), which is a best practice, hence why ESLint is plaining.

You should avoid unnecessary mutations/side-effects whenever possible in your code to prevent bugs and create an overall more efficient program architecture.

More specifically in this case..

ESLint: Disallow Reassignment of Function Parameters (no-param-reassign)

const sortingAlgo = (a, b) => {
    // we need to use custom sorting algorithm to meet our requirements.
    let aPos = a.pos.toUpperCase(),
        bPos = b.pos.toUpperCase();

    if (aPos.slice(0, 1) === bPos.slice(0, 1)) {
        if (parseInt(aPos.slice(1)) < parseInt(bPos.slice(1))) {
            return -1;
        }
        if (parseInt(aPos.slice(1)) > parseInt(bPos.slice(1))) {
            return 1;
        }
        return 0;
    } else {
        if (parseInt(aPos.slice(0, 1)) < parseInt(bPos.slice(0, 1))) {
            return -1;
        }
        if (parseInt(aPos.slice(0, 1)) > parseInt(bPos.slice(0, 1))) {
            return 1;
        }
        return 0;

    }
}

You ESLint is configured to not allow re-assignment of parameters, i.e if a function takes 2 arguments (a, b), then you CANT re-assign these 2 variables in the function body.

To fix this error, either:

  • disable the eslint rule (in your .eslintrc or just for the function)
  • create new variables to avoid re-assigning to a and b (see below):
 sortingAlgo = (a, b) => {
        // we need to use custom sorting algorithm to meet our requirements.
        const aPos = a.pos.toUpperCase();
        const bPos = b.pos.toUpperCase();
        ...

Then replace references to a.pos and b.pos to aPos and bPos.

本文标签: javascriptAssignment to property of function parameter 39a39 EslintStack Overflow