-1

I have this code below :

function myFunction(){ 
  let events_array = [];
  events_array[0] =   [ 'vahsonqmvl80jligsncdb7amk0@google.com', 'Oregon IRC', '6/1/2022','Old' ];
  events_array[1] =   [ 'vahsonqmvl80jligsncdb7amk0@google.com', 'Oregon IRC', '6/1/2023','Old' ];
  events_array[2] =   [ 'vahsonqmvl80jligsncdb7amk0@google.com', 'Oregon IRC', '6/1/2024','Old' ];

  new_element = [ 'vahsonqmvl80jligsncdb7amk0@google.com', 'Oregon IRC', '6/1/2023','Old' ];

  if(new_element in events_array){
    console.log('Exist');
  }else{
    console.log('Does not exist');
  }
}

I want to identify if the array new_element is in the array new_events. Is there a fast way to do it without looping manually like 'not in'? The general for loop is very slow it causes my code to exceed the maximum execution time. I tried the code above but it always say 'does not exist'. Please help. Thanks!

alyssaeliyah
  • 2,214
  • 6
  • 33
  • 80
  • 1
    You could show us your 'general for loop'-approach and we could perhaps find a reason why it would exceed the maximum execution time. Eventually, after you strip away all syntactic sugar, it will boil down to loops, until a condition is met. – Marco Aug 24 '22 at 05:43
  • 1
    Are the values in the `events_array[n]` ordered? Will they always have the same structure or do we need to order them ourself when comparing? – Marco Aug 24 '22 at 05:46
  • 1
    Well, first off, you are trying to compare two objects... which is why you are continually receiving "Does Not Exist" as the return result. An array is technically an object, and you can't === compare an object and expect it to return true. You'd have to loop through the main array, while also looping through each element in the array you are on (say `array[0]...[1]` etc, checking each element of each array with the `new_element` array. – DOZBORNE Aug 24 '22 at 05:49
  • @Marco - the code I posted above is a simplified form of my complex code. I need to do it in a fast way since Google appscript maximum execution is only 6 minutes. – alyssaeliyah Aug 24 '22 at 05:49
  • There is no "Fast" way really. To properly ensure your validation is correct, you'd have to do a doubly nested for loop. Which should be far less than 6 minutes! Unless you have a trillion items to loop through. Most optimal time here is (i'm just guessing off the top) O(n^2). You have to loop through each element in each array you are checking – DOZBORNE Aug 24 '22 at 05:51
  • Darn, just as I was about to post an answer, this gets closed down. Well: compare the length of each element to new_element. Whjen this is true, go over each element of new_element and check the index of that element inside your `events_array[n]`. When all comes up green, you have found a match. – Marco Aug 24 '22 at 05:58

1 Answers1

1

Try this, maybe it's faster...

events_array.findIndex(i => i[0] === new_element[0] && i[1] === new_element[1] && i[2] === new_element[2] && i[3] === new_element[3]) >= 0
AurysVrV
  • 67
  • 1
  • 10