我有以下代码,它充当Google电子表格的自定义函数:
// counts the instances of elem in an array called list
function countInstances(elem, list) {
var p = 0;
for (var i = 0; i < list.length; i++) {
if (list[i] == elem) p++;
}
return p
}
function J(E) {
//var E = ["a","a","a","a","b","b","b","b","c","c","c","c"];
var elems = E.getUnique();
var b;
return countInstances(elems[0], E);
}
这似乎是直截了当的,但发生了一些奇怪的事情:
修改
getUnique函数
Array.prototype.getUnique = function(){
var u = {}, a = [];
for(var i = 0, l = this.length; i < l; ++i){
if(u.hasOwnProperty(this[i])) {
continue;
}
a.push(this[i]);
u[this[i]] = 1;
}
return a;
}
编辑2:
您可以找到示例电子表格here。