return Array to global variable

radiotonga's icon

Hello, everybody. I've been struggling with this thing for a while now, trying to process data inside different functions and returning the results as arrays to a global variable array ("sad_array"). However, I noticed that I don't get an array by the return method, instead I just receive a generic object. This can be seen in the code written below.

Is there a way to do this correctly, so I can actually receive an array instead of an object? Or if I should make it into array, how should that be? I've tried with some methods that are apparently not supported in Max ("Array.from()" for instance). The thing is that Max keeps posting it as a reference error when I try to use the array properties of "sad_array", like length. Sometimes it still works, but I keep getting those damn messages.

Thank you very much.

inlets = 1;
outlets = 1;

var sad_array = [];

function bang() {
    sad_array = make_me_happy();
    test();
}

function test() {
    if (sad_array.isArray) {
        outlet(0, typeof sad_array);
    } else {
        outlet(0, typeof sad_array + " is no happy array");
    }
}

function make_me_happy() {
    var prozac = [0, 3, 4, 2, 1];
    return prozac;
    }

Ben Bracken's icon

This works for me:
if (Array.isArray(sad_array)) {}

You can also use instanceof Array or Object.prototype.toString...

if (sad_array instanceof Array) {}

or a bit slower:

if (Object.prototype.toString.call(sad_array) == "[object Array]") {}

-Ben

radiotonga's icon

Thanks Ben, it's working here too.