0

I have an array where I send a set of values after an operation on a spreadsheet followed by taking the average. Now I want to return each row also along with the above data.

I thought of using two-dimensional arrays. But I have less clarity in implementing this.


for (var i = 0; i < spreadsheetRows.length; i++)
{
    //operations done and variables updated
    variable1=
    variable2=

    variablen=
}

var sendArray = [];

sendArray.push(variable1);
sendArray.push(variable2);
sendArray.push(variable3);
sendArray.push(variable4);
return sendArray;

Now i want to send the array rowFirst & rowSecond also

for (var i = 0; i < spreadsheetRows.length; i++)
{
    //first row of spreadsheet
    rowFirst=[];  //data of first row
    rowSecond=[]; //data of second row

    //operations done and variables updated
    variable1=
    variable2=

    variablen=

}
    var sendArray = [];
    sendArray.push(variable1);
    sendArray.push(variable2);
    sendArray.push(variable3);
    sendArray.push(variable4);

    sendArray.push(rowFirst); // stuck here <---
    sendArray.push(rowSecond);// stuck here <----
    return sendArray;

How to send the array with these two data( ie rowFirst and rowSecond) . Please guide me.

Output Expected

sendArray=[
    var1,
    var2, 
    var3,
    varn,
    rowFirst=[num1, num2, num3,...numn]
    rowSeocnd=[num1, num2, num3,...numn]
]
3
  • So, you want to add two empty arrays, rowFirst and rowSecond to the sendArray? Commented Sep 20, 2017 at 12:09
  • @BrianBennett - this is what i am looking for - sendArray=[ var1, var2, var3, varn, rowFirst=[num1, num2, num3,...numn], rowSeocnd=[num1, num2, num3,...numn] ] Commented Sep 20, 2017 at 12:39
  • i just want to pass these data to the function. please suggest any possible way Commented Sep 20, 2017 at 12:40

1 Answer 1

1

To answer your immediate question, you can push an array into another array by using square brackets in push.

sendArray.push([rowFirst]);
sendArray.push([rowSecond]);

Based on your comment, you may want to use an Object, not an Array (here's a helpful article on the differences). So, think through why you'd want four variables not associated with anything. Can those be grouped or keyed somehow? There are a number of ways to do this and a simple method is to use dot notation to pair a variable or a data set to an object key.

// declare the object and each array
var sendObject = {}

// from your code...
for (var i = 0; i < spreadsheetRows.length; i++)
{
    //operations done and variables updated
    variable1=
    variable2=

    var rowFirst = [variable1, variable2, ...]
}

// Create the key in the Object and assign the array
sendObject.rowFirst = rowFirst;

The output would be:

sendObject = {
  "rowFirst": [variable1, variable2, ...]
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. @Brian Bennett And when i return in a function how can i separate the values. function myFunc(){ . . . return sendObject; } var CallerObj = myFunc(); CallerObj.rowFirst=?
You can open the object using dot notation and loop through arrays. So, for(var i=0; i<CallerObj.rowFirst.length; i++) { // do stuff... } Take a look at the article I linked. It'll help.
I tried but getting - Uncaught Error: The script completed but the returned value is not a supported return type
You may need to wrap it in JSON.stringify() and open with JSON.parse().

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.