作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这可能不是这里的典型问题,但我确实没有认识的人来检查这个问题。我正在对别人的代码进行一些重构,只是想听听第二个意见,因为我当然认为我的工作让它变得更好,但是来自你们专业人士的一些验证会很有帮助。 p>
从这样的数组开始:
errorLog: [{
errorCode: 11,
errorDescription: "abc",
date: "2017-01-01",
severity: "H"
},{
errorCode: 11,
errorDescription: "abcd",
date: "2017-01-02",
severity: "H"
},{
errorCode: 99,
errorDescription: "abcd",
date: "2017-01-02",
severity: "H"
}]
并尝试获得这样的结果:
errorSummary: [{
errorCode: 11,
severity: "H",
count: 2
},{
errorCode: 99,
severity: "H",
count: 1
}]
这是现有代码:
//instead of this, which is hard to reason about and debug (and includes a line that will never rturn true: if (hardErrorsSorted.includes...)):
let hardErrors = testData.filter(ts1 => ts1.severity === 'H');
let hardErrorsSorted = hardErrors.sort(this.mySorter);
for (let i = 0; i < hardErrorsSorted.length; i++) {
if (i != hardErrorsSorted.length - 1) {
if (hardErrorsSorted[i].errorCode != hardErrorsSorted[i + 1].errorCode) {
let errorCount = this.getCount(hardErrorsSorted, hardErrorsSorted[i].errorCode);
this.errorDataList.push({
errorCode: hardErrorsSorted[i].errorCode,
errorCodeType: 'H',
errorCodeTotalCount: errorCount
});
}
} else {
if (hardErrorsSorted.includes(hardErrorsSorted[i].errorCode, 0)) {
} else {
let errorCount = this.getCount(hardErrorsSorted, hardErrorsSorted[i].errorCode);
this.errorDataList.push({
errorCode: hardErrorsSorted[i].errorCode,
errorCodeType: 'H',
errorCodeTotalCount: errorCount
});
}
}
}
以及我的重构:
//use something like this, which is much easier to grasp at a glance, doesn't jump around, and is DRYer
let hardErrorCodes = testData.filter(ts => ts.severity === 'H').map(v => v.errorCode);
let hardErrorCounts = {};
//sum up the unique errors
for (let error of hardErrorCodes) {
if (!(error in hardErrorCounts)) {
hardErrorCounts[error] = 0;
}
hardErrorCounts[error]++;
}
//add the summed error counts to the master list
for (let error in hardErrorCounts) {
this.errorDataList.push({
errorCode: error,
errorCodeType: "H",
errorCodeTotalCount: hardErrorCounts[error]
});
大家觉得怎么样?一个有用的重构?还是浪费时间?
最佳答案
你甚至可以更进一步:
const result = [], ids = {};
for(const {errorCode, severity} of hardErrorCodes){
if(severity !== "H") continue;
if(ids[errorCode]){
ids[errorCode].count++;
}else{
result.push( ids[errorCode] = { severity, errorCode, count: 1});
}
}
这使其速度提高了 3 倍(理论上)
如果您不想过滤严重性:
for(const {errorCode, severity} of hardErrorCodes){
if(ids[severity + errorCode]){
ids[severity + errorCode].count++;
}else{
result.push( ids[severity + errorCode] = { severity, errorCode, count: 1});
}
}
关于javascript - 这是对总结对象数组有益的 JavaScript 重构吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47184116/
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我正在查看另一个开发人员提交给项目的差异,他们有很多代码可以执行 !! .事实上,这似乎是他们实现 boolean getter 和 setter 的标准模式。他们已经实现了他们的代码: - (BOO
假设我有一个显示博客文章的网站。在这个网页上有一个评论部分,但它在页面加载时隐藏了。但是,评论计数会在其位置显示给用户,如果用户单击该计数,则会进行 ajax 调用以加载所有评论。 通常我会做一些额外
一般推荐使用ConfigureAwait(false)在不需要上下文的情况下等待异步调用时。只是想知道使用 ConfigureAwait(false) 有什么好处吗?在 Azure 函数中。 Azur
我是一名优秀的程序员,十分优秀!