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.
2 Answers
Reset to default 3There'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
版权声明:本文标题:javascript - Assignment to property of function parameter 'a' Eslint - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741794271a2397841.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论