Home:ALL Converter>Filtering an array based on another Array in Google Apps Script using a 'broad match'-style filtering method

Filtering an array based on another Array in Google Apps Script using a 'broad match'-style filtering method

Ask Time:2020-05-16T17:19:09         Author:jonasjohajohanson

Json Formatter

I'm fairly new to JavaScript and could need some help with the following problem: Namely, filtering my data in Google Apps Script using an array as input for items I'd like to remove/filter from my data. How to do that correctly I figured out thanks to the help of @Cooper who provided the correct answer in the following thread:

Filtering an array based on another Array in Google Apps Script

However, and this is important, I'd like to filter my data not only on exact matches but broad matches, too. The code bellow does filter my data correctly, but only excludes exact matches. For example, if my array toExclude contains 'red', all rows with the word 'red' are excluded. That's good news. But rows with, for example, 'red wine' remain in my data set. And that's what I'd like to change.

The data I'm working with looks like this and some example items I'd like to filter/removed are specified bellow:

function main() {

// create some example data.
var data = [ [ 3, 15, 52 ],
           [ 'red wine', 18, 64 ],
           [ 'blue', 11, 55 ],
           [ 'shoes', 9, 18 ],
           [ 'car door', 7, 11 ],
           [ 50, 34, 30 ],
           [ 'house party', 10, 17 ],
           [ 'party', 12, 13 ],
           [ 'cheap beer', 30, 15 ] ];


// define filtered items.
var toExclude = [ 3, 'red', 'door', 'party', '' ];


// run the filter provided by @Cooper.
  var d=0;
  for(var i=0;i-d<data.length;i++) {
    for(var j=0;j<data[i-d].length;j++) {
      if(toExclude.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);
}

Here's what my output looks like supposed to what it should look like:

// how the output actually looks.
 [ [ 'red wine', 18, 64 ],         // supposed to be filtered out since 'red' is included.
  [ 'blue', 11, 55 ],
  [ 'shoes', 9, 18 ],
  [ 'car door', 7, 11 ],
  [ 50, 34, 30 ],
  [ 'house party', 10, 17 ],      // supposed to be filtered out since 'party' is included.
  [ 'cheap beer', 30, 15 ] ]

// how it should look.
 [[ 'blue', 11, 55 ],
  [ 'shoes', 9, 18 ],
  [ 'car door', 7, 11 ],
  [ 50, 34, 30 ],
  [ 'cheap beer', 30, 15 ] ]

Does anyone know how to solve my problem? I know the problem lies in the way the command works. Specifically that I check if the previously defined variable toExcludeis part of my data and thus remove only rows where the output is TRUE, which is only if an exact match occurs. How can I change that? I know it is possible with single inputs but can't apply this logic to this rather complex code above.

Thanks for your help!

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/61834513/filtering-an-array-based-on-another-array-in-google-apps-script-using-a-broad-m
yy