Forums > Javascript

javascript dict.get something::else?

September 25, 2012 | 12:07 pm

I can’t tell if this is exactly the same issue as discussed here http://www.cycling74.com/forums/topic.php?id=41755 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:

– Pasted Max Patch, click to expand. –

`
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");


September 25, 2012 | 3:57 pm

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;
}

September 25, 2012 | 4:36 pm

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");


September 30, 2012 | 11:17 am

function bang()
{
var dict_conversions = new Dict("conversions");

if (dict_conversions.contains("kepToVG"))
{

var obj = dict_conversions.get("kepToVG");

if (obj instanceof Dict)
{
var data = obj.get("q");

post("q: ", data, "n");

}
}

}

[attachment=204879,4473]

Attachments:
  1. scshotmaxjs.png

October 2, 2012 | 9:28 pm

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.

Attachments:
  1. test.zip

November 26, 2012 | 6:36 pm

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?


Viewing 6 posts - 1 through 6 (of 6 total)