javascript dict.get something::else?
I can't tell if this is exactly the same issue as discussed here https://cycling74.com/forums/accessing-a-dictionary-from-an-array-of-dictionaries so apologies for duplication.
I am trying to access one of my dictionaries in JavaScript. At the moment it's a bunch of conversion tables that seemed easier to put in a single dictionary but this is something that I anticipate doing a lot, as I do in regular patches. Am I doing something wrong or is this a bug?
Essentially when I attempt myDictionary.get("level1::level2") as formatted for replace in the help file I get a jsobject where I would expect to see the value. Example below.
Patcher/dictionary:
`
contents of getting.js:
conversions = new Dict("conversions");
test = new Dict();
post (conversions.getkeys(),"n");
//all seems to work as expected
path = "something";
post (path, ":", conversions.get(path),"n");
path = "kepToVG";
post (path, ":", conversions.get(path),"n");
//this is what I'm trying to avoid but it's a workaround
test = conversions.get(path);
post(test.get("q"),"n");
//going down one level futher
path = "kepToVG::q";
//replacing as in the help file seems to work fine
conversions.replace(path, "testing");
// am I doing something wrong here? I would expect to see the value
post (path, ":", conversions.get(path),"n");
i believe these issues are related to javascript behavior that will return an object instead of an array, if the array only contains a single element.
see if this code helps:
function dict_to_jsobj(dict) {
var o = new Object();
var keys = dict.getkeys();
if (keys instanceof Array) {
for (var i = 0; i < keys.length; i++)
{
var value = dict.get(keys[i]);
if (value && value["push_to_coll"]) {
value = dict_to_jsobj(value);
}
o[keys[i]] = value;
}
} else {
var value = dict.get(keys);
o[keys] = value;
}
return o;
}
Thanks for your reply. I think I have an idea of what's going on, but I'm not entirely sure if I understand how to implement - do you mean for me to use that function directly, perhaps like this? Using code above as example as that's where I've just copied and tested it:
path = "kepToVG::w";
output = conversions.get(path);
post (path, ":", dict_to_jsobj(output),"n");
Thanks both for your help. I'm still struggling with this problem and wondering if you'd be able to clarify further.
I've tried looking at the first suggestion but either it hasn't worked out or I haven't been able to figure out how to use or adapt it. Please could you let me know?
I'm still not actually sure if I'm supposed to be able to use dict.get with an argument formatted like so:
mydictionary.get("level1::level2::value")
I've been playing around a bit and trying to work out where it might be going wrong and have come up with the attached patch, just looking at where the process ends up by looking at the type and the keys if it is a dictionary. Rather than the result I'm expecting, it appears to be returning a reference to the dictionary containing it.
Just bumping this thread as I still haven't managed to resolve it and struggling to move forward with the project - could anyone give any advice please?
In spite of the four years passed, this maybe useful for someone having the same issue:
function fetch(a,b){
var dict_conversions = new Dict("conversions");
var keys = dict_conversions.getkeys();
post("keys: ", keys, "\n");
if (dict_conversions.contains(keys[a])) {
post("key: ", keys[a], "\n");
var obj = dict_conversions.get(keys[a]);
if (obj instanceof Dict) {
var subkeys = obj.getkeys();
post("subkeys: ", subkeys, "\n");
var data = obj.get(subkeys[b]);
post("data: ", data, "\n");
}
}
}
Hello Pedro, How would one implement the above code into a patch? i have a similar problem as the op and am really quite baffled as to how to solve it.. I’m trying to unpack all the nutritional info from a food API as integers for sonification and am getting some results fine but not all as expected. Hopefully you can pont me in the right direction :)
{
"patcher" : {
"fileversion" : 1,
"appversion" : {
"major" : 7,
"minor" : 3,
"revision" : 2,
"architecture" : "x64",
"modernui" : 1
}
,
"rect" : [ 42.0, 85.0, 977.0, 617.0 ],
"bglocked" : 0,
"openinpresentation" : 0,
"default_fontsize" : 12.0,
"default_fontface" : 0,
"default_fontname" : "Arial",
"gridonopen" : 1,
"gridsize" : [ 15.0, 15.0 ],
"gridsnaponopen" : 1,
"objectsnaponopen" : 1,
"statusbarvisible" : 2,
"toolbarvisible" : 1,
"lefttoolbarpinned" : 0,
"toptoolbarpinned" : 0,
"righttoolbarpinned" : 0,
"bottomtoolbarpinned" : 0,
"toolbars_unpinned_last_save" : 0,
"tallnewobj" : 0,
"boxanimatetime" : 200,
"enablehscroll" : 1,
"enablevscroll" : 1,
"devicewidth" : 0.0,
"description" : "",
"digest" : "",
"tags" : "",
"style" : "redness",
"subpatcher_template" : "",
"boxes" : [ {
"box" : {
"id" : "obj-28",
"maxclass" : "dict.view",
"numinlets" : 1,
"numoutlets" : 0,
"patching_rect" : [ 142.25, 664.5, 274.0, 130.0 ],
"style" : ""
}
}
, {
"box" : {
"id" : "obj-15",
"linecount" : 4,
"maxclass" : "message",
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "" ],
"patching_rect" : [ 455.0, 474.5, 164.0, 64.0 ],
"style" : "",
"text" : "obj:dictionary:u450007194 obj:dictionary:u946007193 obj:dictionary:u287007192 obj:dictionary:u414007191"
}
}
, {
"box" : {
"fontname" : "Arial",
"fontsize" : 13.0,
"id" : "obj-12",
"maxclass" : "newobj",
"numinlets" : 1,
"numoutlets" : 1,
"outlettype" : [ "" ],
"patching_rect" : [ 455.0, 423.0, 133.0, 23.0 ],
"style" : "",
"text" : "dict.unpack nutrients:"
}
}
, {
"box" : {
"id" : "obj-10",
"maxclass" : "message",
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "" ],
"patching_rect" : [ 211.916656, 602.0, 171.0, 22.0 ],
"style" : "",
"text" : "132."
}
}
, {
"box" : {
"id" : "obj-9",
"maxclass" : "message",
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "" ],
"patching_rect" : [ 68.0, 575.0, 171.0, 22.0 ],
"style" : "",
"text" : "\"Cheese, cheddar\""
}
}
, {
"box" : {
"fontname" : "Arial",
"fontsize" : 13.0,
"id" : "obj-4",
"maxclass" : "newobj",
"numinlets" : 1,
"numoutlets" : 3,
"outlettype" : [ "", "", "" ],
"patching_rect" : [ 57.0, 515.5, 200.0, 23.0 ],
"style" : "",
"text" : "dict.unpack foods: name: weight:"
}
}
, {
"box" : {
"id" : "obj-34",
"maxclass" : "newobj",
"numinlets" : 2,
"numoutlets" : 2,
"outlettype" : [ "", "" ],
"patching_rect" : [ 359.0, 376.0, 109.0, 22.0 ],
"style" : "",
"text" : "route report::foods"
}
}
, {
"box" : {
"id" : "obj-8",
"maxclass" : "message",
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "" ],
"patching_rect" : [ 269.666656, 446.0, 173.0, 22.0 ],
"style" : "",
"text" : "dictionary u365007189"
}
}
, {
"box" : {
"id" : "obj-6",
"maxclass" : "message",
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "" ],
"patching_rect" : [ 318.25, 288.0, 98.0, 22.0 ],
"style" : "",
"text" : "get report::foods"
}
}
, {
"box" : {
"id" : "obj-5",
"maxclass" : "message",
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "" ],
"patching_rect" : [ 91.666656, 460.0, 50.0, 22.0 ],
"style" : "",
"text" : "\"28\""
}
}
, {
"box" : {
"id" : "obj-2",
"maxclass" : "newobj",
"numinlets" : 2,
"numoutlets" : 2,
"outlettype" : [ "", "" ],
"patching_rect" : [ 117.666656, 376.0, 89.0, 22.0 ],
"style" : "",
"text" : "route report::sr"
}
}
, {
"box" : {
"id" : "obj-1",
"maxclass" : "message",
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "" ],
"patching_rect" : [ 86.666656, 288.0, 79.0, 22.0 ],
"style" : "",
"text" : "get report::sr"
}
}
, {
"box" : {
"id" : "obj-16",
"linecount" : 2,
"maxclass" : "message",
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "" ],
"patching_rect" : [ 196.666656, 446.0, 50.0, 36.0 ],
"style" : "",
"text" : "\"All foods\""
}
}
, {
"box" : {
"id" : "obj-64",
"maxclass" : "newobj",
"numinlets" : 2,
"numoutlets" : 2,
"outlettype" : [ "", "" ],
"patching_rect" : [ 220.166656, 376.0, 115.0, 22.0 ],
"style" : "",
Cheers!
Hi Rob!
Following is my original test patch.
For it to work, you just have to copy the JavaScript code inside the [js] object.
Regarding your code, it seems to be for [dict] but I can't get Max to accept it - maybe it has some sintax error?!
Hello Pedro!
Thanks for the quick response!
You were right about the error, have re-attached the working (i hope) patch .
I'm trying to unpack each set of nutritional info data and extract the numbers/info
Going to have a poke around your test patch tonight and see if i can figure out how to proceed :)
i'll get there...