Reputation: 1816
I'm working with a super-small library that builds key/value objects. The libraries code can be found here https://github.com/scaraveos/keyval.js/blob/master/keyval.js
There's a number of ready-built functions for setting/deleting keys and values. However there's no function for updating a given key.
I'm trying to create one, but not having much luck. This is what I'm trying to do.
var kv = new keyval();
var key = "This is a key";
var key2 = "test";
var value = "This is the value";
kv.set(key, value);
var reset = kv.set(key, key2);
var get1 = kv.get(key);
var get2 = kv.get(key2);
document.querySelector("div").innerHTML = get1.v;
document.querySelector("div").innerHTML = get2.v;
and here's a fiddle http://jsfiddle.net/mmK9V/
If someone can help me on this, I'd be super appreciative.
Upvotes: 0
Views: 771
Reputation: 21807
You should be able to do this by creating a new keyvalue object with key2,value1 and then removing key1 from the container:
function resetKey(kv, key1, key2) {
var value1 = kv.get(key1);
kv.set(key2, value1);
kv.del(key1);
}
After calling this function, key1 does not exist in the container and instead you have key2 which has the same value that key1 used to have.
Upvotes: 1