json loop through keys code example

Example 1: loop through json object javascript

var p = {
    0: "value1",
    "b": "value2",
    key: "value3"
};

for (var key of Object.keys(p)) {
    console.log(key + " -> " + p[key])
}

Example 2: loop through json object javascript

var p = {
    "p1": "value1",
    "p2": "value2",
    "p3": "value3"
};

for (var key in p) {
    if (p.hasOwnProperty(key)) {
        console.log(key + " -> " + p[key]);
    }
}

Example 3: going through every attributes of an object javascript

let a = {x: 200, y: 1}
let attributes = Object.keys(a)
console.log(attributes)
//output: ["x", "y"]

Example 4: loop through json array and get key name

function iterateNodes(data) {
    for (var i = 0, l = data.nodes.length; i < l; i++) {
        var node = data.nodes[i];

        console.log(node.name);

        if (node.nodes) {
            arguments.callee(node);
        }
    }
}

iterateNodes(data);

Example 5: loop though json object in javascript

var p = {
    "p1": "value1",
    "p2": "value2",
    "p3": "value3"
};