Object Handling in JavaScript
Declaration and Update
Object Declaration
- Using object literal:
{}
- Using constructor:
new Object()
Accessing Values
- Dot notation:
object.key
- Bracket notation:
object["key"]
Check if a Key Exists
- Using
hasOwnProperty
:object.hasOwnProperty("key")
- By retrieving the value:
if (object.key !== undefined)
Delete a Property
delete object.key
Iterating Through an Object
- Using
for...in
loop:for (let key in object) {
if (object.hasOwnProperty(key)) {
console.log(key, object[key]);
}
}
Object Methods
Object.keys()
- Description: Returns all enumerable keys of the given object (excludes Symbol keys).
- Example:
Object.keys({ a: 1, b: 2 }); // ["a", "b"]
Object.values()
- Description: Returns all enumerable values of the given object (excludes Symbol keys).
- Example:
Object.values({ a: 1, b: 2 }); // [1, 2]
Object.assign()
- Description: Copies the properties of one or more source objects to a target object.
- Example:
let target = { a: 1 };
let source = { b: 2 };
Object.assign(target, source); // { a: 1, b: 2 }
Spread Operator
- Description: Concatenates objects without mutating the original objects.
- Example:
let obj1 = { a: 1 };
let obj2 = { b: 2 };
let combined = { ...obj1, ...obj2 }; // { a: 1, b: 2 }
Object.create()
- Description: Creates a new object with a specified prototype.
- Example:
let proto = { a: 1 };
let obj = Object.create(proto); // obj inherits properties from proto
Object.defineProperty()
- Description: Defines a property with specific descriptors (e.g., writable, enumerable, configurable).
- Example:Refer: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty
Object.defineProperty(obj, "key", {
value: 10,
writable: false,
enumerable: true,
configurable: false,
get() {
return key;
},
set(val) {
key = val;
},
});
Object.defineProperties()
- Description: Defines multiple properties with descriptors.
- Example:
Object.defineProperties(obj, {
key1: { value: 10, writable: true },
key2: { value: 20, enumerable: false },
});
Object.propertyIsEnumerable()
- Description: Checks if a property is enumerable.
- Example:
obj.propertyIsEnumerable("key"); // true/false
Object.entries()
- Description: Converts an object into an array of key-value pairs.
- Example:
Object.entries({ a: 1, b: 2 }); // [["a", 1], ["b", 2]]
Object.fromEntries()
- Description: Converts an array of key-value pairs into an object.
- Example:
Object.fromEntries([
["a", 1],
["b", 2],
]); // { a: 1, b: 2 }
Object.freeze()
- Description: Freezes an object, making it immutable.
- Example:
Object.freeze(obj);
Object.isFrozen()
- Description: Checks if an object is frozen.
- Example:
Object.isFrozen(obj); // true/false
Object.preventExtensions()
- Description: Prevents new properties from being added to an object.
- Example:
Object.preventExtensions(obj);
Object.isExtensible()
- Description: Checks if an object is extensible.
- Example:
Object.isExtensible(obj); // true/false
Object.seal()
- Description: Seals an object, preventing new properties and making existing properties non-configurable.
- Example:
Object.seal(obj);
Object.isSealed()
- Description: Checks if an object is sealed.
- Example:
Object.isSealed(obj); // true/false
Object.getOwnPropertyDescriptor()
- Description: Returns the descriptor of a specific property.
- Example:
Object.getOwnPropertyDescriptor(obj, "key");
Object.getOwnPropertyDescriptors()
- Description: Returns descriptors of all properties.
- Example:
Object.getOwnPropertyDescriptors(obj);
Object.getOwnPropertyNames()
- Description: Returns an array of all property names (including non-enumerable properties).
- Example:
Object.getOwnPropertyNames(obj);
Object.getOwnPropertySymbols()
- Description: Returns only symbol keys of an object.
- Example:
Object.getOwnPropertySymbols(obj);
Object.getPrototypeOf()
- Description: Returns the prototype of an object.
- Example:
Object.getPrototypeOf(obj);
Object.is()
- Description: Compares two objects. Returns
true
only if they are the same instance. - Example:
Object.is(obj1, obj2); // true/false
Object.setPrototypeOf()
- Description: Sets the prototype of an object. (Note: Avoid this for performance reasons.)
- Example:
Object.setPrototypeOf(obj, proto);