Google App Script breaking singular element into 1D array with several elements

I've created a custom script in GAS (for Google Sheets) so I could join several data sources into one unique display. I added a .splice in the middle so I could cut out all null elements inside the arrays (which meant removing blank rows in the return array).

Here is it how the code goes:

function COMPILER(){

var i;
var c;
var r;
var display = [];

for (i = 0; i < arguments.length; i++) {
for (c = 0; c < arguments[i].length;c++) {

display.push(arguments[i][c]);

};};

for (r = display.length-1; r >= 0; r--) {

if (display[r][1]==""){

display.splice(r, 1)

};};

return display

};

The code works fine with 2+D arrays and with 1D arrays that have 2+ elements; but when its working with 1D arrays that have only one element, it unintendedly breaks down the strings into several elements.

Instead of returning:

ar1
ar2

It returns

a
r
1
a
r
2

How could I solve this?

Upvotes: 0

Views: 153

Answers (1)

IMTheNachoMan
IMTheNachoMan

Reputation: 5811

I think/assume this is what you're trying to do:

function COMPILER()
{
    var display = [];

    for(var i = 0, numArguments = arguments.length; i < numArguments; ++i)
    {
        //console.log([i, arguments[i], Array.isArray(arguments[i])]);
        if(Array.isArray(arguments[i]))
        {
            for(var j = 0, len = arguments[i].length; j < len; ++j)
            {
                if(arguments[i][j] != "")
                {
                    display.push(arguments[i][j]);
                }
            }
        }
        else if(arguments[i] != "")
        {
            display.push(arguments[i]);
        }
    }

    return display;
}

I can't confirm/test cause I don't know how you're calling COMPILER.

Upvotes: 1

Related Questions