Write a function that takes an object as a parameter and prints all its property names and values. Create a function called ‘printObjectProperties’ that takes an object as a parameter. Inside the function, use a loop to iterate through the object properties and print each property name and its value in the following format: “Property: <property name>, Value: <value>” Using the following object as test input for the function:
let person = {
firstName: 'John',
lastName: 'Doe',
age: 30,
address: {
street: '123 Main St',
city: 'Anytown',
zipCode: '12345'
},
hobbies: ['reading', 'swimming', 'coding'],
isStudent: false,
};
this problem is very tough pl solve it accordingly without using Jason stringy got the following code already but my tool is not accepting this code..so pl give an alternative solution to the image question. Thank You
function printObjectProperties(obj) {
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
console.log(`Property: ${key}, Value: ${JSON.stringify(obj[key])}`);
}
}
}
// Test the function with the provided object
let person = {
firstName: 'John',
lastName: 'Doe',
age: 30,
address: {
street: '123 Main St',
city: 'Anytown',
zipCode: '12345'
},
hobbies: ['reading', 'swimming', 'coding'],
isStudent: false
};
printObjectProperties(person);
user25613389 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.