JavaScript – How do I loop through an object

Given an object as following:

var o = {
    "k1": "value1",
    "k2": "value2",
    "k3": "value3"

To loop through it, then do following:

for (var key in o) {
    if (o.hasOwnProperty(key)) {
        console.log(key + " - " + JSON.stringify(o[key]));
k1 - "value1"
k2 - "value2"
k3 - "value3"

MDN Web Docs -Object.prototype.hasOwnProperty()

Author: Wu Chia Chong

My main areas of interest are software architectures and software design methods, patterns, and new trends in software development.