databasegoogle-apps-scriptdatabase-scripts

Google ScriptDB: How to retrieve a Key Value Pair using a variable?


I am using Google UI Apps and ScriptDB, and I have a map object similar to this:

myMapObject = {record_id: projectA,
               apple : 316,
               orange : 956,
               banana : 536}

I am wondering how to use a variable in a query to retrieve the key value pair data. Or, is it even possible? For example:

var userChoice = e.parameter.choice;
var userLimit = e.parameter.limit;
var result = db.query({userChoice : db.greaterThan(userLimit)})

Now, I know using a variable works for the conditional statement, db.greaterThan(userLimit). However, I haven't been able to use a varriable (i.e. "userChoice") in the way I have written it for the key value.

Note, that this is only a simplified example of the code and I am not looking for a way to restruction the map object. What I would like to know is, if it is possible to use a varriable in some way to perform the query. Thanks for your help.


Solution

  • You can use a variable as property, but it's a different syntax. Like this:

    var query = {};
    query[userChoice] = db.greaterThan(userLimit);
    //edit as requested in the comment
    query.record_id = 'projectA'; //or query['record_id'] = ...
    var result = db.query(query);