Home:ALL Converter>Filtering an array based on another Array in Google Apps Script

Filtering an array based on another Array in Google Apps Script

Ask Time:2020-05-09T07:09:22         Author:jonasjohajohanson

Json Formatter

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.

Thanks!

Author:jonasjohajohanson,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/61689567/filtering-an-array-based-on-another-array-in-google-apps-script
Farnoosh :

I'd make an object with all your unwanted keys, then I'd use them to filter your data array.\n\nconst ObjectOfKeys = keys.reduce((acc,rec)=> {\n let nextAcc = acc;\n nextAcc[rec] = true;\n return nextAcc;\n },{}};\n\nconst filteredData = data.filter(rec => !ObjectKeys[rec[0]])\n\n\nalternatively approach, you could skip making the object if your keys were unique. \n\nconst filterData = data.filter(rec => !keys.includes(rec))\nNote. this will not work if for example your key is 'apple' and data[0] is 'a', so first approach would ensure no such conditions throw the code off. ",
2020-05-08T23:35:31
Cooper :

Try this:\n\nfunction myfunction() {\n var keys = [['a','b','c']][0];//flatten\n var data = [['a',1,2],['c',4,3],['d',3,4]];\n var d=0;\n for(var i=0;i-d<data.length;i++) {\n for(var j=0;j<data[i-d].length;j++) {\n if(keys.indexOf(data[i-d][j])!=-1) {\n data.splice(i-d++,1);//remove the row and increment the delete counter\n break;//break out of inner loop since row is deleted\n }\n }\n }\n console.log(data);\n}\n\n\nThanks 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.\n\nCooper,1.171\nCooper,1.195\nFarnoosh,1.2\nFarnoosh,1.224\nCooper,1.237\nTheMaster,1.257\nCooper,1.264\nFarnoosh,1.347\nTheMaster,1.371\nTheMaster,1.392\nCooper,1.503\nFarnoosh,1.513\nFarnoosh,1.563\nTheMaster,1.699\nTheMaster,1.936\n",
2020-05-08T23:28:50
TheMaster :

Create a Set of the keys and use set.has:\n\n\r\n\r\n// elements to filter out.\r\nconst keys = [['a','b','c']],\r\nkeySet = new Set(keys[0]),\r\n\r\n// example data.\r\ndata = [['a',1,2],\r\n ['c',4,3],\r\n ['d',3,4]],\r\nout = data.filter(([item0])=>!keySet.has(item0));\r\nconsole.info(out);",
2020-05-09T05:29:04
yy