JS Traversing Object Entries
const profile={
name:'Aryan Kumar',
age:20,
sex:'Male',
married:true,
}
for(let key in profile) {
console.log(key, profile[key]);
}
// Another Way
console.log(Object.keys(profile));
console.log(Object.values(profile));
//Another Way
for(let val of Object.values(profile)){
console.log(val)
}
// Anotehr Exercies
const salaries={
Bhushan:5999,
Birkha:4500,
Roshan:4000,
Suman:6432,
}
let sum=0;
for(let key in salaries){
console.log(key, salaries[key]);
sum=sum+salaries[key];
}
console.log('Total Salary =' +sum);
// Other Example
let total=0;
for (let val of Object.values(salaries)){
total +=val;
}
console.log(total);
Comments
Post a Comment