Javascript 如何改进MODE.MULT的这种实现
我编写了一个相对复杂的MicrosoftExcel函数实现,它返回一个数组,数组中出现频率最高的值或重复值。它是用三个循环实现的,包括一个嵌套到另一个循环中,我怀疑有一种更简单的方法来实现它。作为信息,它使用from从返回的数组中提取重复值Javascript 如何改进MODE.MULT的这种实现,javascript,excel,underscore.js,excel-formula,lodash,Javascript,Excel,Underscore.js,Excel Formula,Lodash,我编写了一个相对复杂的MicrosoftExcel函数实现,它返回一个数组,数组中出现频率最高的值或重复值。它是用三个循环实现的,包括一个嵌套到另一个循环中,我怀疑有一种更简单的方法来实现它。作为信息,它使用from从返回的数组中提取重复值 function MODEMULT(range) { var n = range.length; var max = 0; var counts = new Array(); var result = new Array(); for (
function MODEMULT(range) {
var n = range.length;
var max = 0;
var counts = new Array();
var result = new Array();
for (var i = 0; i < n; i++) {
counts[i] = 0;
for (var j = 0; j < n; j++) {
if (range[j] === range [i]) {
counts[i]++;
max = (counts[i] > max) ? counts[i] : max;
}
}
}
for (var k = 0; k < n; k++) {
if (counts[k] === max) {
result.push(range[k]);
}
}
return _.uniq(result);
}
出于测试目的,MODEMULT[1,2,3,4,3,2,1,2,3]应返回[2,3]
提前感谢您的帮助 您可以使用更少的循环,尽管这会影响内存使用,因为您将保留原始范围内所有唯一项的计数:
function MODEMULT(range) {
var n = range.length,
// object to hold the number of occurrences per entry
count= {},
// array to hold those numbers which have the current max occurrences
maxItems = [],
// reference to the current maximum
max = 0,
// JSLint placement of currentItem used inside the loop
currentItem;
for (var i = 0; i < n; i++) {
currentItem= range[i];
// Update the occurrences table.
count[currentItem] = count[currentItem] ? count[currentItem] + 1 : 1;
// If a new maximum is created, void the original maxItems and update max.
if (count[currentItem] > max) {
max = count[currentItem];
maxItems = [];
}
// If the entry reaches the current max, add it to maxItems.
if (count[currentItem] === max) {
maxItems[maxItems.length] = currentItem;
}
}
// No need for unique, as a duplicate value
// will increase max and thus empty out maxItems.
return maxItems;
}
您可以使用更少的循环,尽管这会影响内存使用,因为您将保留原始范围内所有唯一项的计数:
function MODEMULT(range) {
var n = range.length,
// object to hold the number of occurrences per entry
count= {},
// array to hold those numbers which have the current max occurrences
maxItems = [],
// reference to the current maximum
max = 0,
// JSLint placement of currentItem used inside the loop
currentItem;
for (var i = 0; i < n; i++) {
currentItem= range[i];
// Update the occurrences table.
count[currentItem] = count[currentItem] ? count[currentItem] + 1 : 1;
// If a new maximum is created, void the original maxItems and update max.
if (count[currentItem] > max) {
max = count[currentItem];
maxItems = [];
}
// If the entry reaches the current max, add it to maxItems.
if (count[currentItem] === max) {
maxItems[maxItems.length] = currentItem;
}
}
// No need for unique, as a duplicate value
// will increase max and thus empty out maxItems.
return maxItems;
}
令人惊叹的正是我想要的。非常感谢你的帮助。我更新了我的代码,并在感谢ismael上给了你荣誉。斯多葛看起来很棒!在相当长的一段时间里,我一直在为基于使用ApachePOI的定制工具的项目自动进行Excel->Javascript转换,作为构建阶段,但从未见过类似的项目。一个真正伟大的发现!谢谢很高兴你喜欢。对于一些更复杂的函数,特别是缺少的统计函数,我们可以使用一些帮助-令人惊叹的正是我想要的。非常感谢你的帮助。我更新了我的代码,并在感谢ismael上给了你荣誉。斯多葛看起来很棒!在相当长的一段时间里,我一直在为基于使用ApachePOI的定制工具的项目自动进行Excel->Javascript转换,作为构建阶段,但从未见过类似的项目。一个真正伟大的发现!谢谢很高兴你喜欢。对于一些更复杂的函数,特别是缺少的统计函数,我们可以使用一些帮助-