javascript foreach object keys 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: object values javascript
const object1 = {
a: 'somestring',
b: 42,
c: false
};
console.log(Object.values(object1));
Example 3: javascript foreach object keys
var lunch = {
sandwich: 'ham',
snack: 'chips',
drink: 'soda',
desert: 'cookie',
guests: 3,
alcohol: false,
};
Object.keys(lunch).forEach(function (item) {
console.log(item);
console.log(lunch[item]);
});
Example 4: javascript foreach object key
const obj = {
name: 'Jean-Luc Picard',
rank: 'Captain'
};
Object.keys(obj).forEach(key => {
console.log(key, obj[key]);
});
Example 5: foreach object javascript
const obj = {
a: "aa",
b: "bb",
c: "cc",
};
for(let key in obj) {
console.log(key);
console.log(obj[key]);
}
Example 6: javascript iterate through object attributes
for (var prop in obj) {
if (Object.prototype.hasOwnProperty.call(obj, prop)) {
}
}