mrphuzz
mrphuzz

Reputation: 199

function parameter as json key

I'm not a JSON god by any stretch of the imagination, but I'm not understanding why this isn't working, and what I need to do to make it work. I've searched around for a while now and most likely my problem is I don't know the right keywords to search for to solve this. I'm wanting to call a function and, within that function, create a JSON object so I can store it in an IndexedDB "table", like so:

UpdateSettingsValue("initialized", true);

function UpdateSettingsValue(key, value){
    var tx = db.transaction(["settings"], "readwrite");
    var store = tx.objectStore("settings");
    var trans = {
        key:value
    }
    var request = store.add(trans);
}

This is creating an entry just fine, but it gets written as {key: true}.

So my question is how can I make it written as {initialized: true}?

Thanks for your help!

Upvotes: 1

Views: 2825

Answers (1)

Arthur Azevedo De Amorim
Arthur Azevedo De Amorim

Reputation: 23592

Just initialize the trans variable by hand instead of using literal syntax:

function UpdateSettingsValue(key, value){
    var tx = db.transaction(["settings"], "readwrite");
    var store = tx.objectStore("settings");
    var trans = {};
    trans[key] = value;
    var request = store.add(trans);
}

Upvotes: 2

Related Questions