2

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!

1
  • 1
    What logic does the empty string in the toExclude require? The empty string is contained in every item, so it doesn't sound like you want to filter it out. Do you want to filter out only exact matches to the empty string? (but you don't have any first items which are the empty string) Commented May 16, 2020 at 9:26

1 Answer 1

3

You could exclude by checking the values and use Array#filter and Array#some.

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]],
    toExclude = [3, 'red', 'door', 'party', ''],
    result = data.filter(a => 
        !a.some(v => 
            toExclude.some(w => v === w || w && typeof v === 'string' && v.includes(w))
        )
    );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

1 Comment

Hell yeah! Your code runs like a charm. I'll do some further checking to make sure it works for my real life data example. But at first glance it looks like it does. Just one tiny problem, your code ist far too complex vor me to understand.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.