var obj =
{
name:'some name',
otherProperty:'prop value',
date: new Date()
};
for(i in obj)
{
var propVal = obj[i]; // i is the key, and obj[i] is the value ...
}
var global = (function() {
return this;
})();
// Pair object, similar to Python
function Pair(key, value) {
this.key = key;
this.value = value;
this.toString = function() {
return "(" + key + ", " + value + ")";
};
}
/**
* as function
* @param {String} dataName A String holding the name of your pairs list.
* @return {Array:Pair} The data list filled
* with all pair objects.
*/
Object.prototype.as = function(dataName) {
var value, key, data;
global[dataName] = data = [];
for (key in this) {
if (this.hasOwnProperty(key)) {
value = this[key];
(function() {
var k = key,
v = value;
data.push(new Pair(k, v));
})();
}
}
return data;
};
var d = {
'one': 1,
'two': 2
};
// Loop on your (key, list) pairs in this way
for (var i = 0, max = d.as("data").length; i < max; i += 1) {
key = data[i].key;
value = data[i].value;
console.log("key: " + key + ", value: " + value);
}
// delete data when u've finished with it.
delete data;