Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
Object methods are built-in functions that help you work with objects. Instead of writing loops to get keys or values, use these ready-made tools from Object!
Object.keys(). Want to copy? Use Object.assign(). Clean and simple!keys, values, entries
assign, fromEntries
freeze, seal
groupBy
Gets all enumerable property names as an array
const user = {
name: 'Alice',
age: 25,
email: 'alice@example.com'
};
const keys = Object.keys(user);
console.log(keys);
// ['name', 'age', 'email']Working with object keys
Gets all values without needing keys
const scores = {
math: 95,
english: 87,
science: 92
};
const values = Object.values(scores);
console.log(values); // [95, 87, 92]Working with object values
Perfect for looping when you need both key and value
const user = { name: 'Bob', age: 30 };
const entries = Object.entries(user);
console.log(entries);
// [['name', 'Bob'], ['age', 30]]Working with key-value pairs
Copies properties from source objects to target
const target = { a: 1 };
const source = { b: 2, c: 3 };
Object.assign(target, source);
console.log(target);
// { a: 1, b: 2, c: 3 }Copying and merging objects
Creates object from array of [key, value] pairs
const entries = [['name', 'Alice'], ['age', 25]];
const obj = Object.fromEntries(entries);
console.log(obj);
// { name: 'Alice', age: 25 }Building objects from entries
No adding, deleting, or modifying properties (shallow only!)
const config = { api: 'v1', timeout: 5000 };
Object.freeze(config);
config.api = 'v2'; // Ignored (strict mode: error)
config.newProp = 'test'; // Ignored
delete config.timeout; // Ignored
console.log(config);
// { api: 'v1', timeout: 5000 } (unchanged)Creating immutable objects
New in ES2024 - group array items by a key function
const people = [
{ name: 'Alice', age: 25 },
{ name: 'Bob', age: 30 },
{ name: 'Charlie', age: 25 }
];
const byAge = Object.groupBy(people, person => person.age);
console.log(byAge);
// {
// 25: [{ name: 'Alice', ... }, { name: 'Charlie', ... }],
// 30: [{ name: 'Bob', ... }]
// }Grouping arrays by property (ES2024)
Replaces obj.hasOwnProperty() with safer alternative
const user = { name: 'Alice', age: 25 };
console.log(Object.hasOwn(user, 'name')); // true
console.log(Object.hasOwn(user, 'email')); // falseChecking property existence safely
...obj for shallow copyfor...in without checkshasOwnProperty()Object.groupBy() is very new (ES2024) - check compatibility or use a polyfill for production code!