JUCE Javascript does not support array key/value pairs?

With JUCE Javascript it seems that this functionality is not supported. dynamicTable["mp"] will output a value of 3; no matter what is between the brackets, the last value given is the value outputted.

jsfiddle: https://jsfiddle.net/97vpfwcp/1/

var dynamicTable = [];

dynamicTable["p"] = 0;
dynamicTable["pp"] = 0;
dynamicTable["mp"] = 1;
dynamicTable["f"] = 2;
dynamicTable["fff"] = 3;

alert(dynamicTable["mp"]);

Your code - while technically valid - is “inappropriate usage of Javascript arrays” (see this site - under “What about Associative Arrays?”). Your dynamicTable is an array and not an associative container.

For example, your code will result in an unexpected length of your dynamicTable -> 0.

jsfiddle: https://jsfiddle.net/8w2mjomj/

By doing dynamicTable["p"] = 0; you really are just assigning a property to an object. You can do that to any javascript object - for example a boolean.

how do I accomplish my goal? I need to assign an arbitrary number to a string. (it needs to be as easy and simple as possible)

This doesn’t work either

var table = {
  "pp":0,
  "p":16,
  "mp":4
};

alert(table["p"]);

Surely you should be able to reference properties using dot notation e.g.

alert (table.p);

?

yes that works, but it doesn’t work for my goal.

I need this:

I have a string. “p”. Now what is my corresponding assigned value?

Another way to put it:

my_dynamic = "fff";
dynamic_value = ???; // need to get dynamic value for "fff" here
midi_velocity = dynamic_value * 10;

I could do this:

https://jsfiddle.net/8w2mjomj/2/

dynamicTable = ["p","mp","fff"];
dynamicTableVal = [1,4,5];
alert(dynamicTableVal[dynamicTable.indexOf("fff")]);

but that’s not user friendly. I need a straightforward user friendly way to do this. If there is no way to do this in javascript then just let me know, I’ll see if I can come up with some kind of built-in functions for my program.

You can add bracket subscription to access properties of objects pretty easy.

In juce_Javascript.cpp, modify the ArraySubscript class (starting with line 408):

var getResult (const Scope& s) const override
{
    var arrayVar (object->getResult (s)); // must stay alive for the scope of this method

    if (const Array<var>* array = arrayVar.getArray())
    {
        return (*array) [static_cast<int> (index->getResult (s))];
    }
    else if (const DynamicObject* obj = result.getDynamicObject())
    {
        const String name = index->getResult(s).toString();
        
        return obj->getProperty(Identifier(name));
    }


    return var::undefined();
}

void assign (const Scope& s, const var& newValue) const override
{
    var arrayVar (object->getResult (s)); // must stay alive for the scope of this method

    if (Array<var>* array = arrayVar.getArray())
    {
        const int i = index->getResult (s);
        while (array->size() < i)
            array->add (var::undefined());

        array->set (i, newValue);
        return;
    }
    else if (DynamicObject* obj = result.getDynamicObject())
    {
        const String name = index->getResult(s).toString();
                 
        return obj->setProperty(Identifier(name), newValue);
    }

    Expression::assign (s, newValue);
}

(This is a rather quick hack, there may be some edge cases where the behavior is not standard conform).

Now you can use the Javascript conform array subscript for objects:

var dynamics = {};
dynamics["pp"] = 30;
alert(dynamics["pp"]);
1 Like

Sorry for the late reply. Yesterday, we’ve made some fixes to the javascript interpreter to support this. Jules is currently reviewing the code. We will push this to develop as soon as it is ready.

1 Like

OK this is now on develop.

1 Like