繁体   English   中英

Google表格中MIN()的自定义数组function

[英]Custom array function for MIN() in Google Sheets

Since the MIN() function in Sheets only returns a single value and there is no way to make it work with ARRAYFORMULA, I wanted to make a custom function that would take two arrays and compare the values at each entry, and return an array of最小值。 (我知道有一种使用 QUERY 的解决方法,但它不适用于我的目的)

我现在拥有的将两个 arrays 与一排并完美地工作。 不幸的是,当引入多行时,它会中断。 我不知道为什么,所以我不知道如何前进。 如何使它适用于任何尺寸的 arrays?

当我给它提供任何二维范围时,它会引发错误:

“TypeError:无法设置未定义的属性‘0’”

在这一行finalarray[x][y] = Math.min(arr1[x][y], arr2[x][y]);

当前的“工作”代码:

function MINARRAY(arr1, arr2) {
  if (arr1.length == arr2.length && arr1[0].length == arr2[0].length)
  {    
      var finalarray = [[]];

      for (x = 0; x < arr1.length; x++)
      {     
        for(y = 0; y < arr1[x].length; y++)
        {
          finalarray[x][y] = Math.min(arr1[x][y], arr2[x][y]);
        }            
      }  
      return finalarray;   
  } 
  else
  {    
    throw new Error("These arrays are different sizes");    
  }
}

finalarray是一个二维数组。 [[]]仅将finalarray的第一个元素设置为数组。 需要将finalarray的所有元素设置为一个数组。 在循环内部,添加

finalarray[x]=[]//set `x`th element as a array or finalarray[x]= finalarray[x] || []
finalarray[x][y] = Math.min(arr1[x][y], arr2[x][y]);      

或者,

finalarray[x] = [Math.min(arr1[x][y], arr2[x][y])]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM