javascript iterate over json object code example

Example 1: javascript loop through object example

var person={
 	first_name:"johnny",
  	last_name: "johnson",
	phone:"703-3424-1111"
};
for (var property in person) {
  	console.log(property,":",person[property]);
}

Example 2: how to iterate a json object in javascript

var obj = {a: 1, b: 2};
for (var key in obj) {
  if (obj.hasOwnProperty(key)) {
    var val = obj[key];
    console.log(val);
  }
}

Example 3: iterate over json data javascript

var arr = [ "one", "two", "three", "four", "five" ];
var obj = { one:1, two:2, three:3, four:4, five:5 };

jQuery.each(arr, function() {
  $("#" + this).text("My id is " + this + ".");
  return (this != "four"); // will stop running to skip "five"
});

jQuery.each(obj, function(i, val) {
  $("#" + i).append(document.createTextNode(" - " + val));
});

Example 4: 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 5: loop though json object in javascript

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