When iterating over the members of an object for logging the keys / values one gets a "stair case" effect. Therefore I wrote myself this function which takes care for a left-alignment of the values.
Any hints concerning flaws and improvement-recommendations welcome.
// #### START TEST #######################
var person = {
yourMobilPhoneNumber : 01234171819,
firstName : 'theFirstName',
lastName : 'theLastName',
mail : '[email protected]',
zip : '12345',
street : 'theNameOfMyStreet',
city : 'someCitySomewhere',
yourVeryPersonalWebpage : 'http://that-is-me.com',
id : 12345,
calculate: function() { return 3 + 4; }
};
displayMembers(person);
// #### END TEST #######################
// Displays the members of an assigned
// object on the console.
// -- Parameter -------------
// Object - An object which members
// shall be displayed.
function displayMembers(obj) {
var i;
var max = (function() {
var ret = 0;
var keys = Object.keys(obj);
for (i = 0; i < keys.length; i++) {
if (keys[i].length > ret)
ret = keys[i].length;
}
return ret;
})();
var getSpacer = function(len, state) {
if (state.length < len) {
return getSpacer(len, state += ' ');
} else {
return state;
}
}
for (i in obj) {
console.log('%s: %s%s',
i,
getSpacer(max - i.length, ''),
obj[i]);
}
}