Skip to content

Instantly share code, notes, and snippets.

@bhaireshm
Last active January 8, 2021 05:51
Show Gist options
  • Save bhaireshm/d9a4ff4dc371cea6f391ff35100e10f1 to your computer and use it in GitHub Desktop.
Save bhaireshm/d9a4ff4dc371cea6f391ff35100e10f1 to your computer and use it in GitHub Desktop.
Print the object's key values in aligned way.
/**
* Example Output
*
* "id : PS10140"
* "sdid : SD13112"
* "disableCrud : false"
* "newQueryParameter : true"
*/
var obj = {id: "PS10140", sdid: "SD13112", disableCrud: "false", newQueryParameter: "true"};
/**
* @param obj - accepts only object
*/
printPretty(obj);
function printPretty(obj){
var l = ((o)=>{
var w = 0;
for (var k in o) {
var l = `${k}`.length;
w = l > w ? l : w;
}
return w;
})(obj);
for(var k in obj){
var s = ' '.repeat(l - `${k}`.length);
console.log(`${k}${s} : ${obj[k]}`);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment