Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to return two 2d arrays from a function JavaScript Google script

I have a function that produces two 2d arrays; how do I return both of them?

I can return two 1d arrays

function test() {
  do some stuff and get
  arr1 =["A',"B","C"]
  
  do some other stuff and get
  arr2 =["X","Y","Z"]


  return [arr1,arr2]
}

test[0]= arr1

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

test[1]=arr2

But if I have

function test() {
  do some stuff and get
  ary1 =[
         ["A',"B","C"],
         ["M',"N","O"]
        ] 
         

  do some other stuff and get
  ary2 =[
         ["X","Y","Z"],
         ["M',"N","O"]
        ] 
  return [ary1,ary2]
}

and I get

test[0] =["A’,"B","C"]

test[1] =["X","Y","Z"]

Expected result:

test[0] =[
          ["A',"B","C"],
          ["M',"N","O"]
         ]

test[1] =[
          ["X","Y","Z"],
          ["M',"N","O"]
         ] 

how do I return ary1 and ary2?

>Solution :

I think that in your script, test of test[0] and test[1] is the function. So, in this case, please modify test()[0] and test()[1].

function test() {
  ary1 = [["A", "B", "C"], ["M", "N", "O"]];
  ary2 = [["X", "Y", "Z"], ["M", "N", "O"]];
  return [ary1, ary2];
}

console.log(test()[0]);
console.log(test()[1]);

Note:

  • As additional information, in the case of test()[0] and test()[1], the script of test() is run 2 times. So, in this case, I thought that the following sample might be suitable.
function test() {
  ary1 = [["A", "B", "C"], ["M", "N", "O"]];
  ary2 = [["X", "Y", "Z"], ["M", "N", "O"]];
  return [ary1, ary2];
}

const values = test();
console.log(values[0]);
console.log(values[1]);

// or the following sample can be also used.
const [a, b] = test();
console.log(a);
console.log(b);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading