Q1.Create an object with properties name and age.
const person = {
name: "neetish",
age: 25
}
console.log(person);
Q2. Add a property city to an existing object.
const person = {
name: "neetish",
age: 25
}
person.city = "delhi";
console.log(person);
*Q3. Change the value of the age property to 30. *
const person = {
name: "neetish",
age: 25
}
person.age = 30;
console.log(person);
** Q4. Remove the city property from an object. **
const person = {
name: "John Doe",
age: 30,
city: "New York"
};
delete person.city;
console.log(person);
Q5. Check if a property name exists in an object.
const person = {
name: "John Doe",
age: 30,
city: "New York"
};
if ('name' in person) {
console.log(`Yes! The name is available in the object, and the value of name is ${person.name}.`);
} else {
console.log(`No, name is not available.`);
}
Q6. Get all keys of an object.
const person = {
name: "John Doe",
age: 30,
city: "New York"
};
const keys = Object.keys(person);
console.log(keys);
*Q7. Get all values of an object. *
const person = {
name: 'Alice',
age: 30,
city: 'Wonderland'
};
const values = Object.values(person);
console.log(values);
*Q8. How does using Object.assign() to create a copy of an object affect the properties of the original object when changes are made to the copied object?" *
const person = {
name: 'Alice',
age: 30,
city: 'Wonderland'
};
// Create a shallow copy of the person object
const copyPerson = Object.assign({}, person);
// Log the copied object to the console
console.log(copyPerson); // Output: { name: 'Alice', age: 30, city: 'Wonderland' }
// Modify the age property of the copied object
copyPerson.age = 32;
// Log the modified copied object to the console
console.log(copyPerson); // Output: { name: 'Alice', age: 32, city: 'Wonderland' }
// Log the original person object to the console to show it remains unchanged
console.log(person); // Output: { name: 'Alice', age: 30, city: 'Wonderland' }
*Q9. 9. Use the spread operator to clone an object. *
const person = {
name: 'Alice',
age: 30,
city: 'Wonderland'
};
// Create a shallow clone of the person object using the spread operator
const cloneObj = { ...person };
// Log the cloned object to the console
console.log(cloneObj); // Output: { name: 'Alice', age: 30, city: 'Wonderland' }
Q10. Merge two objects into one using Object.assign().
const obj1 = {
name: 'Alice',
age: 30
};
const obj2 = {
city: 'Wonderland',
age: 32 // This will overwrite the age from obj1
};
// Merging the two objects
const mergedObj = Object.assign({}, obj1, obj2);
console.log(mergedObj); // Output: { name: 'Alice', age: 32, city: 'Wonderland' }
Q11. Nested objects: Create an object with a nested object for address.
const person = {
name: "neetish",
age: "30",
gender: "male",
address: {
houseName: "lakshmi Nilayam #32",
floor: "First Floor",
nearBy: "Kodathi Gate",
city: "bengaluru",
state: "karnataka"
}
}
*Q12. Update a nested property (e.g., change city in address to New York). *
const person = {
name: "neetish",
age: "30",
gender: "male",
address: {
houseName: "lakshmi Nilayam #32",
floor: "First Floor",
nearBy: "Kodathi Gate",
city: "bengaluru", // Original city value
state: "karnataka"
}
};
// Update the city property in the address object
person.address.city = "New York";
// Log the updated person object to the console
console.log(person);
Q13. Add a new property 'zip' to the nested address object
const person = {
name: "neetish",
age: "30",
gender: "male",
address: {
houseName: "lakshmi Nilayam #32",
floor: "First Floor",
nearBy: "Kodathi Gate",
city: "bengaluru",
state: "karnataka"
}
};
// Add a new property 'zip' to the nested address object
person.address.zip = 56099;
// Log the updated person object to the console
console.log(person);
Top comments (0)