根据Google Apps脚本中的另一个数组过滤一个数组 [英] Filtering an array based on another Array in Google Apps Script

查看:42
本文介绍了根据Google Apps脚本中的另一个数组过滤一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚接触JavaScript,可能需要一些帮助来解决我在使用Google Apps脚本时遇到的问题.

I'm fairly new to JavaScript and could need some help with a problem I'm facing while working on a Google Apps Script.

我打算做的是基于数组过滤我的数据,该数组是我从特定工作表中的特定单元格中获取的,其中包含不想保留在数据中的字符串元素.换句话说,包含这些关键字的行应从我的工作表中删除.

What I'm intending to do is to filter my data based on an array, which I grab from a specific cell in a specific sheet, that contains string elements I don't want to keep in my data. In other words, rows that include these keywords should be removed from my sheet.

到目前为止,我已经设法在过滤器函数中使用单个字符串元素来做到这一点.

Thus far I have managed to do this using a single string element in my filter function.

// elements to filter out.
var keys = [['a','b','c']];

// example data.
var data = [['a',1,2],
            ['c',4,3],
            ['d',3,4]];

// my approach for a single element which works.
var filterText = "a";
var newData1 = data.filter(function(item) {
  return item[0] !== filterText;
});
console.log(newData1); // output: [ [ 'c', 4, 3 ], [ 'd', 3, 4 ] ]

但是,如果我尝试将此逻辑扩展到使用数组过滤数据的情况,则会遇到麻烦.我在下面编写了可正确检查我的数据但未返回预期结果的代码.我知道我在这里错过了代码的关键部分,但无法弄清楚它是哪一个.我把代码翻了好几次,但是还是被卡住了.

However, if I try to extend this logic to the case were I'm using an array to filter my data, I'm in trouble. I wrote the code below which correctly checks my data but does not return the expected outcome. I know I'm missing out on a crucial part of code here but can't figure out which one it is. I turned the code around several times but got stuck either way.

for(var n=0; n<keys[0].length; n++) {
  // console.log(keys[0][n]);
  var newData2 = data.filter(function(item) {
    // console.log(item[0] === keys[0][n]);
     return item[0] === keys[0][n];
  })
};
console.log(newData2); // output: [ [ 'c', 4, 3 ] ]

希望你们能为我指明正确的方向,并帮助我了解我在这里犯错的地方.

Hope you guys could point me in the right direction and help me to understand where I'm making my mistake here.

谢谢!

推荐答案

尝试一下:

function myfunction() {
  var keys = [['a','b','c']][0];//flatten
  var data = [['a',1,2],['c',4,3],['d',3,4]];
  var d=0;
  for(var i=0;i-d<data.length;i++) {
    for(var j=0;j<data[i-d].length;j++) {
      if(keys.indexOf(data[i-d][j])!=-1) {
        data.splice(i-d++,1);//remove the row and increment the delete counter
        break;//break out of inner loop since row is deleted
      }
    }
  }
  console.log(data);
}

感谢其他解决方案.我真的需要更多其他数组方法和箭头符号的练习.只是为了踢球,我将数据集的大小增加到了1200行,并排运行了不同的方法,下面是数据的csv.并不是真正的结论.每次都是相同的数据集,我对所有这些都使用console.log输出.数据来自视图执行输出.

Thanks for the other solutions. I really need more practice with other array methods and arrow notation. Just for kicks I increased the dataset size to 1200 lines and ran the different methods side by side and below is a csv of the data. Not really conclusive. It was the same data set each time and I used console.log output on all of them. The data came from the view executions output.

Cooper,1.171
Cooper,1.195
Farnoosh,1.2
Farnoosh,1.224
Cooper,1.237
TheMaster,1.257
Cooper,1.264
Farnoosh,1.347
TheMaster,1.371
TheMaster,1.392
Cooper,1.503
Farnoosh,1.513
Farnoosh,1.563
TheMaster,1.699
TheMaster,1.936

这篇关于根据Google Apps脚本中的另一个数组过滤一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆