从JS数组中删除非0伪造值的最简单方法是什么? [英] What is the simplest method to remove non-0 falsy values from JS array?
问题描述
我正在尝试一种功能,使其能够在Google表格中从上次停止的地方收起".
I am trying to have a function be able to "pick up where it left off" in Google Sheets.
我需要在具有数据的特定列中找到最后一行.但是:
I need to find the last row in a specific column which has data. However:
-
工作表中还有许多其他的列,甚至还有更多的行,所以我不能使用在工作表上的getLastRow.
The sheet has many other columns with even more rows, so I cannot use getLastRow on the Sheet.
我正在尝试确定要定位的范围,因此getLastRow现在将还是在范围级别上工作,因为我还没有范围.
I am trying to determine the range to target, so getLastRow will now work at a range level either, as I do not yet have the range.
我无法处理array.filter(String),因为我正在处理数字值.
I cannot array.filter(String), as I am dealing with numeric values.
我无法过滤删除虚假元素(通常用于过滤空白数组值).
I can not filter to remove falsy elements (often used to filter blank array values).
示例:
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1");
var range = sheet.getRange(B1:B);
var values = range.getValues(); //Pseudo Array values = [0,0,1,"",""]
var quantityOfValues= values.filter(?????).length //Goal: quantityOfValues== 3
var dataRange = sheet.getRange(1,2,quantityOfValues,1).... // do stuff to this range
推荐答案
最简单的删除所有虚假值的方法可以胜过零,您可以检查真实值还是零.
The simplest method to remove all falsy values excelpt zero, you could check for truthy value or for zero.
result = array.filter(v => v || v === 0);
这篇关于从JS数组中删除非0伪造值的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!