13 Object Manipulation questions

neetish_kumar_db8a8a2e8bb

Neetish Kumar

Posted on October 13, 2024

13 Object Manipulation questions

Q1.Create an object with properties name and age.

const person = {
name: "neetish",
age: 25
}
console.log(person);
Enter fullscreen mode Exit fullscreen mode

Q2. Add a property city to an existing object.

const person = {
name: "neetish",
age: 25
}
person.city = "delhi";
console.log(person);
Enter fullscreen mode Exit fullscreen mode

*Q3. Change the value of the age property to 30. *

const person = {
name: "neetish",
age: 25
}
person.age = 30;
console.log(person);
Enter fullscreen mode Exit fullscreen mode

** Q4. Remove the city property from an object. **

const person = {
  name: "John Doe",
  age: 30,
  city: "New York"
};

delete person.city;
console.log(person);
Enter fullscreen mode Exit fullscreen mode

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.`);
}

Enter fullscreen mode Exit fullscreen mode

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);

Enter fullscreen mode Exit fullscreen mode

*Q7. Get all values of an object. *

const person = {
  name: 'Alice',
  age: 30,
  city: 'Wonderland'
};

const values = Object.values(person);
console.log(values);

Enter fullscreen mode Exit fullscreen mode

*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' }

Enter fullscreen mode Exit fullscreen mode

*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' }

Enter fullscreen mode Exit fullscreen mode

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' }

Enter fullscreen mode Exit fullscreen mode

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"
  }
}
Enter fullscreen mode Exit fullscreen mode

*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);

Enter fullscreen mode Exit fullscreen mode

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);

Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
neetish_kumar_db8a8a2e8bb
Neetish Kumar

Posted on October 13, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related