Advanced Object Methods and Operations

Intermediate
110 XP
45 min
Lesson Content

Advanced Object Methods and Operations

JavaScript provides powerful methods for working with objects. These methods make object manipulation more functional and efficient.

Object.keys(), Object.values(), Object.entries()

const person = { name: 'John', age: 30, city: 'NYC' };

// Get all keys
Object.keys(person); // ['name', 'age', 'city']

// Get all values
Object.values(person); // ['John', 30, 'NYC']

// Get key-value pairs
Object.entries(person); // [['name', 'John'], ['age', 30], ['city', 'NYC']]

Object.assign() and Spread Operator

// Object.assign() - Copy properties
const target = { a: 1 };
const source = { b: 2, c: 3 };
Object.assign(target, source); // { a: 1, b: 2, c: 3 }

// Spread operator (preferred)
const merged = { ...target, ...source };
const copy = { ...person };

Object.freeze() and Object.seal()

// Object.freeze() - Prevents all changes
const obj = { name: 'John' };
Object.freeze(obj);
obj.name = 'Jane'; // Ignored in strict mode

// Object.seal() - Prevents adding/deleting, but allows modifying
const obj2 = { name: 'John' };
Object.seal(obj2);
obj2.name = 'Jane'; // Works
obj2.age = 30; // Ignored

Object.hasOwnProperty() and 'in' Operator

const obj = { name: 'John' };

// Check if property exists
obj.hasOwnProperty('name'); // true
'name' in obj; // true

// Check if property doesn't exist
!('age' in obj); // true

Object Destructuring

const person = { name: 'John', age: 30, city: 'NYC' };

// Basic destructuring
const { name, age } = person;

// With default values
const { name, age, country = 'USA' } = person;

// Renaming
const { name: fullName } = person;

// Nested destructuring
const user = { profile: { name: 'John', age: 30 } };
const { profile: { name } } = user;
Example Code

Use advanced object methods to manipulate objects

const user = {
  id: 1,
  name: 'John Doe',
  email: 'john@example.com',
  age: 30,
  active: true
};

// Get keys
const keys = Object.keys(user);
console.log('Keys:', keys);

// Get values
const values = Object.values(user);
console.log('Values:', values);

// Get entries
const entries = Object.entries(user);
console.log('Entries:', entries);

// Check property existence
console.log('Has name:', user.hasOwnProperty('name'));
console.log('Has phone:', 'phone' in user);

// Create copy with spread
const userCopy = { ...user, city: 'NYC' };
console.log('Copy:', userCopy);

// Destructuring
const { name, email, status = 'active' } = user;
console.log('Name:', name);
console.log('Email:', email);
console.log('Status:', status);

// Merge objects
const additional = { role: 'admin', permissions: ['read', 'write'] };
const merged = { ...user, ...additional };
console.log('Merged:', merged);

// Object from entries
const newObj = Object.fromEntries([
  ['a', 1],
  ['b', 2],
  ['c', 3]
]);
console.log('From entries:', newObj);

Expected Output:

Keys: [id,name,email,age,active]
Values: [1,John Doe,john@example.com,30,true]
Entries: [[id,1],[name,John Doe],[email,john@example.com],[age,30],[active,true]]
Has name: true
Has phone: false
Copy: {id:1,name:John Doe,email:john@example.com,age:30,active:true,city:NYC}
Name: John Doe
Email: john@example.com
Status: active
Merged: {id:1,name:John Doe,email:john@example.com,age:30,active:true,role:admin,permissions:[read,write]}
From entries: {a:1,b:2,c:3}
Study Tips
  • Read the theory content thoroughly before practicing
  • Review the example code to understand key concepts
  • Proceed to the Practice tab when you're ready to code