JS Object Management
In this page:
Object.assign(target, source);
Object.keys(object);
Object.entries(object);
delete object.property;
Merging Objects with Object.assign()
Object.assign(target, source1, source2, ...) copies every enumerable own property from each source object into the target object, in order -- properties from later sources overwrite same-named ones from earlier sources, and the modified target is both mutated and returned.
Example: Merging Objects with Object.assign()
// Declare the constant `defaults`, set to `{ theme: "light" }`
// Declare the constant `defaults`, set to `{ theme: "light" }`
const defaults = { theme: "light" };
// Declare the constant `overrides`, set to `{ theme: "dark" }`
// Declare the constant `overrides`, set to `{ theme: "dark" }`
const overrides = { theme: "dark" };
// Declare the constant `merged`, set to `Object.assign({}, defaults, overrides)`
// Declare the constant `merged`, set to `Object.assign({}, defaults, overrides)`
const merged = Object.assign({}, defaults, overrides);
// Print `merged` to the console
// Print `merged` to the console
console.log(merged);
Merging Objects with Spread Syntax
The spread operator inside an object literal, {...obj1, ...obj2}, achieves the same merging behavior as Object.assign() with more concise, readable syntax -- and always creates a brand-new object, never mutating any of the source objects.
Example: Merging Objects with Spread Syntax
// Declare the constant `defaults`, set to `{ theme: "light" }`
// Declare the constant `defaults`, set to `{ theme: "light" }`
const defaults = { theme: "light" };
// Declare the constant `overrides`, set to `{ theme: "dark" }`
// Declare the constant `overrides`, set to `{ theme: "dark" }`
const overrides = { theme: "dark" };
// Declare the constant `merged`, set to `{ ...defaults, ...overrides }`
// Declare the constant `merged`, set to `{ ...defaults, ...overrides }`
const merged = { ...defaults, ...overrides };
// Print `merged` to the console
// Print `merged` to the console
console.log(merged);
The Shallow Copy Limitation
Both Object.assign() and spread syntax perform a shallow copy -- top-level properties are genuinely copied, but if a property's value is itself an object or array, only the reference to that nested structure is copied, meaning the original and the copy still share and can mutate the same nested data.
Example: The Shallow Copy Limitation
const original = { info: { city: "Delhi" } };
const copy = { ...original };
copy.info.city = "Mumbai";
console.log(original.info.city); // "Mumbai" - nested object shared
Building an Object from Entries with Object.fromEntries()
Object.fromEntries(arrayOfPairs) is the reverse operation of Object.entries() -- it takes an array of [key, value] pairs and builds a plain object from them, useful after transforming data with array methods (like filter or map) and wanting the result back as an object.
Example: Building an Object from Entries with Object.fromEntries()
const pairs = [["a", 1], ["b", 2]];
console.log(Object.fromEntries(pairs));
Checking Property Existence and Removing Properties
in and hasOwnProperty() both check whether a property exists on an object, and delete removes a property from an object entirely -- three complementary tools for managing an object's shape beyond just reading and setting values.
Example: Checking Property Existence and Removing Properties
// Declare the constant `user`, set to `{ name: "Sam" }`
// Declare the constant `user`, set to `{ name: "Sam" }`
const user = { name: "Sam" };
// Print `"name" in user` to the console
// Print `"name" in user` to the console
console.log("name" in user);
// Print `user.hasOwnProperty("name")` to the console
// Print `user.hasOwnProperty("name")` to the console
console.log(user.hasOwnProperty("name"));
delete user.name;
// Print `user` to the console
// Print `user` to the console
console.log(user);
- Assuming Object.assign() or spread syntax performs a deep copy -- both only copy properties one level deep, so a nested object or array is still shared by reference between the original and the copy.
- Forgetting that later arguments to Object.assign() (or later spread values) overwrite earlier ones when property names collide, and getting the intended merge priority backward.
- Using Object.assign(target, source) and forgetting that it mutates the target object directly -- passing an empty object literal as the first argument avoids accidentally mutating an existing object.
- Object.assign(target, ...sources) copies properties from one or more source objects into a target object, and returns that target.
- The spread operator ({...obj1, ...obj2}) offers similar merging behavior with more concise syntax.
- Both Object.assign() and spread perform a shallow copy -- nested objects/arrays remain shared by reference, not duplicated.
Object.assign() and object spread syntax are both supported in every modern browser (spread syntax for objects specifically since ES2018).
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: