The following code expects that all the properties of this.triggeredTests are arrays and calculates their total length:
let savedLen = 0;
const values = Object.values(this.triggeredTests);
for (let i = 0; i < values.length; i += 1) {
savedLen += values[i].length;
}
if I try to rewrite this code with for ... in:
for (const val in values) {
savedLen += val.length;
}
I got some annoying errors:
✘ http://eslint.org/docs/rules/guard-for-in The body of a for-in should be wrapped in an if statement to filter unwanted properties from the prototype
src\components\entity\Test.vue:171:9
for (const val in values) {
^
✘ http://eslint.org/docs/rules/no-restricted-syntax for..in loops iterate over the entire prototype chain, which is virtually never what you want. Use Object.{keys,values,entries}, and iterate over the resulting array
src\components\entity\Test.vue:171:9
for (const val in values) {
^
✘ 2 problems (2 errors, 0 warnings)
Errors:
1 http://eslint.org/docs/rules/no-restricted-syntax
1 http://eslint.org/docs/rules/guard-for-in
Is it possible to make this for ... in work?
I am to sure what compiler I use, but at least I know that I am working in Node.js v8.12.0 environment with NPM.
EDIT1:
If I use for ... of:
for (const val of values) {
savedLen += val.length;
}
I am getting another error:
✘ http://eslint.org/docs/rules/no-restricted-syntax iterators/generators require regenerator-runtime, which is too heavyweight for this guide to allow them. Separately, loops should be avoided in favor of array iterations
src\components\entity\Test.vue:171:9
for (const val of values) {
^
EDIT2:
This does not produce errors:
const values = Object.values(this.triggeredTests);
const sumCalculator = (accumulator, currentValue) => accumulator + currentValue.length;
const savedLen = values.reduce(sumCalculator, 0);