在 Javascript 中求两个杠杆数组对象的平均值
Find average of two lever array objects in Javascript
我正在使用 for 循环使用二级数组查找解锁项目的平均值。我只想知道在 ES6 中这样做是否更好?
let pct_completed = 0;
let total_units_of_module = 0;
const topics = _.topics || [];
for(let topic of topics) {
const units = topic.units ||[];
total_units_of_module += units.length;
for (let unit of units) {
if (!unit.lock) pct_completed++;
}
}
pct_completed = Math.floor(pct_completed / total_units_of_module * 100);
我们可以使用 reduce
函数计算相同的值吗?
可能 reduce
在外部阵列上,但我真的不推荐它:
const { pct_completed, total_units_of_module } = topics.reduce((a, topic) => {
const units = topic.units || [];
a.total_units_of_module += units.length;
for (let unit of units) {
if (!unit.lock) a.pct_completed++;
}
return a;
}, { pct_completed: 0, total_units_of_module: 0 });
pct_completed = Math.floor(pct_completed / total_units_of_module * 100);
IMO 的一种更好的方法是计算出有多少个未锁定的单元,并一次添加它们而不是单独递增。 pct_completed
在迭代期间也不是一个好的变量名,因为它代表解锁计数,而不是完成百分比:
let unlockCount = 0;
let total_units_of_module = 0;
for (const topic of _.topics || []) {
const units = topic.units || [];
total_units_of_module += units.length;
unlockCount += units.filter(unit => !unit.lock).length;
}
const pct_completed = Math.floor(unlockCount / total_units_of_module * 100);
你 可以 使用 .reduce
来计算要添加到 unlockCount
的多少,但它看起来有点混乱,IMO 这不值得:
unlockCount += units.reduce((a, unit) => a + !unit.lock, 0);
虽然可能不是性能最高的并且不使用 reduce,但我们至少可以使代码更清晰:
- 我们要计算单位
- 我们要计算已解锁的单位
const units = topics.flatMap(topic => topic.units || [])
const unlockedUnits = units.filter(u => !u.lock)
const pct_completed = units.length
? Math.floor(unlockedUnits.length / units.length * 100)
: 100
我正在使用 for 循环使用二级数组查找解锁项目的平均值。我只想知道在 ES6 中这样做是否更好?
let pct_completed = 0;
let total_units_of_module = 0;
const topics = _.topics || [];
for(let topic of topics) {
const units = topic.units ||[];
total_units_of_module += units.length;
for (let unit of units) {
if (!unit.lock) pct_completed++;
}
}
pct_completed = Math.floor(pct_completed / total_units_of_module * 100);
我们可以使用 reduce
函数计算相同的值吗?
可能 reduce
在外部阵列上,但我真的不推荐它:
const { pct_completed, total_units_of_module } = topics.reduce((a, topic) => {
const units = topic.units || [];
a.total_units_of_module += units.length;
for (let unit of units) {
if (!unit.lock) a.pct_completed++;
}
return a;
}, { pct_completed: 0, total_units_of_module: 0 });
pct_completed = Math.floor(pct_completed / total_units_of_module * 100);
IMO 的一种更好的方法是计算出有多少个未锁定的单元,并一次添加它们而不是单独递增。 pct_completed
在迭代期间也不是一个好的变量名,因为它代表解锁计数,而不是完成百分比:
let unlockCount = 0;
let total_units_of_module = 0;
for (const topic of _.topics || []) {
const units = topic.units || [];
total_units_of_module += units.length;
unlockCount += units.filter(unit => !unit.lock).length;
}
const pct_completed = Math.floor(unlockCount / total_units_of_module * 100);
你 可以 使用 .reduce
来计算要添加到 unlockCount
的多少,但它看起来有点混乱,IMO 这不值得:
unlockCount += units.reduce((a, unit) => a + !unit.lock, 0);
虽然可能不是性能最高的并且不使用 reduce,但我们至少可以使代码更清晰:
- 我们要计算单位
- 我们要计算已解锁的单位
const units = topics.flatMap(topic => topic.units || [])
const unlockedUnits = units.filter(u => !u.lock)
const pct_completed = units.length
? Math.floor(unlockedUnits.length / units.length * 100)
: 100