基于ESLint规则和最佳实践的Javascript循环重构
我需要帮助将我的代码重构为ES6最佳实践,并通过以下ESLinting规则。下面是我的全部方法。这里的for循环就是问题所在基于ESLint规则和最佳实践的Javascript循环重构,javascript,for-loop,ecmascript-6,eslint,Javascript,For Loop,Ecmascript 6,Eslint,我需要帮助将我的代码重构为ES6最佳实践,并通过以下ESLinting规则。下面是我的全部方法。这里的for循环就是问题所在 formatNumber(val) { if (!isFinite(val)) { return val; } const valFloat = val.toString(); const decPos = valFloat.indexOf('.'); const intPart = (decPos === -1) ? valFloat : v
formatNumber(val) {
if (!isFinite(val)) {
return val;
}
const valFloat = val.toString();
const decPos = valFloat.indexOf('.');
const intPart = (decPos === -1) ? valFloat : valFloat.substr(0, decPos);
const formattedNum = [];
// this needs refactoring
for (const ii in intPart) {
if (ii % 3 === 0 && ii !== 0) {
formattedNum.push();
}
formattedNum.push(intPart[intPart.length - 1 - ii]);
}
formattedNum.reverse();
return (decPos === -1) ? formattedNum.join('') : formattedNum.join('') + valFloat.slice(decPos, valFloat.length);
}
首先,在心里解析代码并不是特别有趣:如果你能说出你的代码在做什么,而不是把它留给我们来解析,这会很有帮助,特别是当它在数组中循环键,对键进行计算,然后使用数组中的倒数值时。非常不传统 问题在于循环中的“
for.”,如中所述。此ESLint规则要求,如果在
循环中为..使用,则在每个键上运行Object.prototype.hasOwnProperty
,以确保它是所需对象的一部分,而不是来自向Object.prototype
添加属性的其他代码(在本例中,也可以是String.prototype
)。这是明智的防御性编码
所以你可以这样做:
但对我来说,这并不是一个好的解决方案,事实上,以一种更好、更容易理解的方式重构代码也可以解决这个问题:
const formattedNum = intPart
.split('') // split the string into an array of digits
.reduceRight((acc, val, idx) => { // loop through the array with a reducer function, starting at the right
acc.unshift(val); // add the digit to the beginning of the output array
if (
idx && // if it's not the first item in the array (where idx === 0)
((intPart.length - idx) % 3 === 0) // and it is a multiple of three from the end of the array
) {
acc.unshift(','); // add a comma to the beginning of the array
}
return acc;
}, []);
首先,在心里解析代码并不是特别有趣:如果你能说出你的代码在做什么,而不是把它留给我们来解析,这会很有帮助,特别是当它在数组中循环键,对键进行计算,然后使用数组中的倒数值时。非常不传统
问题在于
循环中的“for.”,如中所述。此ESLint规则要求,如果在
循环中为..使用,则在每个键上运行Object.prototype.hasOwnProperty
,以确保它是所需对象的一部分,而不是来自向Object.prototype
添加属性的其他代码(在本例中,也可以是String.prototype
)。这是明智的防御性编码
所以你可以这样做:
但对我来说,这并不是一个好的解决方案,事实上,以一种更好、更容易理解的方式重构代码也可以解决这个问题:
const formattedNum = intPart
.split('') // split the string into an array of digits
.reduceRight((acc, val, idx) => { // loop through the array with a reducer function, starting at the right
acc.unshift(val); // add the digit to the beginning of the output array
if (
idx && // if it's not the first item in the array (where idx === 0)
((intPart.length - idx) % 3 === 0) // and it is a multiple of three from the end of the array
) {
acc.unshift(','); // add a comma to the beginning of the array
}
return acc;
}, []);
您当前提议的程序仅根据(大概)您的区域设置“格式化”一个数字。我的建议是不要修复正在运行的程序
完全走错了路。考虑和您的<代码>格式号< /代码>函数消失的必要性-
num.toLocaleString([locales[,options]])
使用locales
参数-
var编号=123456.789;
//德语使用逗号作为十进制分隔符,句点表示千
console.log(编号:tolocalstring('de-de'));
// → 123.456,789
//大多数阿拉伯语国家的阿拉伯语使用东方阿拉伯数字
控制台日志(编号为toLocaleString('ar-EG'));
// → ١٢٣٤٥٦٫٧٨٩
//印度使用千/十万/百万分之二的分离器
console.log(编号:tolocalString('en-IN'));
// → 1,23,456.789
//nu扩展键要求一个编号系统,如中文十进制
console.log(编号:tolocalstring('zh-Hans-CN-u-nu-hanidec');
// → 一二三,四五六.七八九
//请求可能不受支持的语言时,例如
//巴厘语,包括备用语言,在本例中为印度尼西亚语
log(number.tolocalString(['ban','id']);
// → 123.456789
您当前提议的程序仅根据(大概)您的区域设置“格式化”一个数字。我的建议是不要修复正在运行的程序
完全走错了路。考虑和您的<代码>格式号< /代码>函数消失的必要性-
num.toLocaleString([locales[,options]])
使用locales
参数-
var编号=123456.789;
//德语使用逗号作为十进制分隔符,句点表示千
console.log(编号:tolocalstring('de-de'));
// → 123.456,789
//大多数阿拉伯语国家的阿拉伯语使用东方阿拉伯数字
控制台日志(编号为toLocaleString('ar-EG'));
// → ١٢٣٤٥٦٫٧٨٩
//印度使用千/十万/百万分之二的分离器
console.log(编号:tolocalString('en-IN'));
// → 1,23,456.789
//nu扩展键要求一个编号系统,如中文十进制
console.log(编号:tolocalstring('zh-Hans-CN-u-nu-hanidec');
// → 一二三,四五六.七八九
//请求可能不受支持的语言时,例如
//巴厘语,包括备用语言,在本例中为印度尼西亚语
log(number.tolocalString(['ban','id']);
// → 123.456789
您是否阅读了例如?ESlint规则页面都有传递代码的示例。请尝试将
中的替换为中的,例如for(intPart的常量ii)您是否阅读了例如?ESlint规则页面都有传递代码的示例。请尝试将中的替换为中的替换为,例如(intPart的常量ii)
const formattedNum = intPart
.split('') // split the string into an array of digits
.reduceRight((acc, val, idx) => { // loop through the array with a reducer function, starting at the right
acc.unshift(val); // add the digit to the beginning of the output array
if (
idx && // if it's not the first item in the array (where idx === 0)
((intPart.length - idx) % 3 === 0) // and it is a multiple of three from the end of the array
) {
acc.unshift(','); // add a comma to the beginning of the array
}
return acc;
}, []);