1

I have 2 array and i want find in first array have value of second array is push data of first array but i can't.

Error message : "TypeError: Cannot find function includes in object...."

This my code:

function loadArrInArr(){

  var firstArr = ["aaa", "bbb", "ccc"];
  var ss = SpreadsheetApp.openByUrl(urldb);
  var ws = ss.getSheetByName("sheet1");
  var secondArr = ws.getDataRange().getDisplayValues();
  var tmpArr = [];

  for(var i = 0; i < secondArr.length; i++){
    if(firstArr.includes(secondArr[i][0])){ //err this line
      tmpArr.push(secondArr[i]);
    }
  }

}
2
  • I found this thread. I think that this thread is useful for your situation. stackoverflow.com/q/50281560/7108653 Commented Aug 15, 2019 at 6:09
  • @Tanaike tks you so much Commented Aug 15, 2019 at 6:15

1 Answer 1

1

Unfortunately, in the current stage, includes() cannot be used with Google Apps Script yet. So how about this modification? In this modification, indexOf is used instead of includes.

Pattern 1:

When your script is modified, how about this modification?

From:

if(firstArr.includes(secondArr[i][0])){

To:

if(firstArr.indexOf(secondArr[i][0]) > -1){

or

if(~firstArr.indexOf(secondArr[i][0])){

Pattern 2:

As another pattern, how about this modification?

From:

var tmpArr = [];

for(var i = 0; i < secondArr.length; i++){
  if(firstArr.includes(secondArr[i][0])){ //err this line
    tmpArr.push(secondArr[i]);
  }
}

To:

var tmpArr = secondArr.filter(function(row) {return ~firstArr.indexOf(row[0])});
  • When the length of secondArr is large, to use filter() can reduce the process cost than that of the for loop.

References:

If I misunderstood your question and this wat not the result you want, I apologize.

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

5 Comments

Thanks for your help, I tested it and succeeded. You're so good !
@Naoa Lee Thank you for replying. I'm glad your issue was resolved. I added one more script. Could you please confirm it? The additional script is faster than the script using the for loop.
Tks you so much, Please give me the link !
@Naoa Lee The additional script is "Pattern 2" in my answer.
I'm sorry, ^^ Thank you for your enthusiasm !

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.