[3 Bugs] Weird behaviour of one element array and array of arrays in Dict and JS

Artur Dobija's icon

1) 1 — E L E M E N T A R R A Y T O J S
I have the following [dict] content:
{
"key" : [ {
"subkey" : 0
} ]
}

When I ask in js:
Array.isArray( dict.get("key") );
I am getting 0.
But I should get 1, because "key" actually IS an array!

This is extremely frustrating because sometimes I need to remove items/keys from my arrays in dict, I end with 1-element array and the code stops working because it expects an array.

I know that I could use an array check like
var key = Array.isArray(key) ? key : [key]

But why should I if actually IT IS an array?

2) S E N D I N G 1 – E L E M A R R A Y F R O M J S T O D I C T
The same behaviour provides the following problem:
In JS I write:
dict.set("key", [ elem ] );
And in Dict I get:
{
"key" : "elem"
}

instead of:
{
"key" : [ "elem" ]
}

What is the reasoning behind that? Is it a bug? Or am I doing something wrong?
One of the workarounds at the end are written here: https://x37v.com/x37v/max/dict-object/ (with append. method) but at best it creates 2-element array and the second element is empty ("" or undefined).

3) A R R A Y O F A R R A Y S
This structure should be possible in JSON:
{
"key" : [ [ 0, 1, 2 ], [ "a", "b", "c" ] ]
}

it could be saved in a Dict etc and error is not reported.
But when I try to access it by JS:
var key = dict.get( "key[0][1]" );
then Max crashes!

* * ~ ~ * ~ * ~ ~ * *
I would appreciate any suggestions, advices or comments about this topic! Thank you!

tyler mazaika's icon

This is how max patches (non JS) treat list values for dict keys as well. If you need single item array capability you may have more luck working with regular JS (Json) objects and using stringify() /parse() to go back and forth with Dict data type when necessary.

Artur Dobija's icon

Thanks for the answer! There is a promise in the dict help file that "dictionaries are fully supported and accessible in the js environment,as demonstrated by this script". Which actually is not to be the case...

Are there any drawbacks of the method you mentioned regarding arrays of objects? I know that someone here wrote a function which converts dicts to objects and objects to dicts, but it does not support arrays of dicts.