for each key object javascript 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: foreach key value javascript
const object1 = {
a: 'somestring',
b: 42
};
for (let [key, value] of Object.entries(object1)) {
console.log(`${key}: ${value}`);
}
Example 3: javascript foreach object key
const obj = {
name: 'Jean-Luc Picard',
rank: 'Captain'
};
Object.keys(obj).forEach(key => {
console.log(key, obj[key]);
});