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
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]andtest()[1], the script oftest()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);