Nesting Arrays in Dict from JS

Jeff Merkel's icon

I'm having trouble understanding what I'm doing wrong here. Basically I'm try to put an array into an array, then push to dict. The nesting works fine if I manually create the format in dict, but there's something happening in the js object or my code/logic that's not working and I get a bunch of "jsobject" with a number. I'm guessing I'm doing something wrong in my code/logic, and probably pretty obvious. Could someone take a look a look at what I'm doing wrong? Many thanks!

autowatch = 1;
var d = new Dict("answers");

Max Patch
Copy patch and select New From Clipboard in Max.

function bang() {
    //Build 10 examples
    for (i=1; i
    var outerArray = []
    //add 5 sets of a basic array to outerArray
    for (j=0; j
        outerArray[j] = [4, 6];
        }
    d.replace("Example " + i, outerArray); //post to dict
    }
}

Jeff Merkel's icon

Or even much simpler, removing dict out of the equation and just posting to the Max console, why does this not work?

function bang() {
    var first = [1, 2];
    var second = [3, 4];
    var nested = [first, second];
    post(first);
    post();
    post(second);
    post();
    post(nested);
    post();
}

Jeff Merkel's icon

The above code works fine in Node and the browser console, by the way, showing the "nested" array as expected [[1, 2], [3, 4]].

Floating Point's icon

here's a clue [JSON.stringify()]:

function bang() {
var first = [1, 2];
var second = [3, 4];
var nested = [first, second];
post(first);
post();
post(second);
post();
post(nested);
post();
post(JSON.stringify(nested));
post();
}

Jeff Merkel's icon

Hey Floating Point! Thanks so much for the tip! That is so rad, but unfortunately, as the name somewhat implies, the output is converted to a string. I can't seem to find a way to stringify actual nested values, not a string representation of it unless there's a [fromsymbol] equivalent in JS.

My workaround is to just use objects instead of arrays. It's just harder to do some of the shuffling and sorting I was planning on doing. Thanks again!