js loop over object properties code example

Example 1: how to loop object javascript

var obj = {a: 1, b: 2, c: 3};

for (const prop in obj) {
  console.log(`obj.${prop} = ${obj[prop]}`);
}

// Salida:
// "obj.a = 1"
// "obj.b = 2"
// "obj.c = 3"

Example 2: 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 3: javascript iterate through object attributes

for (var prop in obj) {
    if (Object.prototype.hasOwnProperty.call(obj, prop)) {
        // do stuff
    }
}

Example 4: javascript loop through object properties

for (var prop in obj) {
    if (Object.prototype.hasOwnProperty.call(obj, prop)) {
        // do stuff
    }
}

Tags:

Php Example