10 Useful JavaScript Object Methods
JavaScript objects are fundamental data structures that store key-value pairs. They come with built-in methods that provide powerful functionalities for manipulating and working with objects. In this post, we'll explore 10 useful JavaScript object methods with syntax and examples to help you become more efficient in your JavaScript development.
1. Object.keys()
Return an array of the enumerable property names of an object.
const vaibhavTiwari = {
name: 'Vaibhav Tiwari',
age: 30,
country: 'India'
};
const keys = Object.keys(vaibhavTiwari);
console.log(keys); // Output: ['name', 'age', 'country']
2. Object.values()
Return an array of the enumerable property values of an object.
const vaibhavTiwari = {
name: 'Vaibhav Tiwari',
age: 30,
country: 'India'
};
const values = Object.values(vaibhavTiwari);
console.log(values); // Output: ['Vaibhav Tiwari', 30, 'India']
3. Object.entries()
Return an array of the enumerable property [key, value] pairs of an object.
const vaibhavTiwari = {
name: 'Vaibhav Tiwari',
age: 30,
country: 'India'
};
const entries = Object.entries(vaibhavTiwari);
console.log(entries);
/* Output:
[
['name', 'Vaibhav Tiwari'],
['age', 30],
['country', 'India']
]
*/
4. Object.assign()
Copy the values of all enumerable properties from one or more source objects to a target object.
const source1 = { a: 1, b: 2 };
const source2 = { b: 3, c: 4 };
const target = { c: 5, d: 6 };
const result = Object.assign(target, source1, source2);
console.log(result); // Output: { a: 1, b: 3, c: 4, d: 6 }
5. Object.freeze()
Freeze an object to make it read-only, preventing any changes to its properties.
const vaibhavTiwari = {
name: 'Vaibhav Tiwari',
age: 30,
country: 'India'
};
Object.freeze(vaibhavTiwari);
vaibhavTiwari.age = 35; // This will have no effect as the object is frozen.
console.log(vaibhavTiwari); // Output: { name: 'Vaibhav Tiwari', age: 30, country: 'India' }
6. Object.fromEntries()
Create an object from an array of key-value pairs (entries).
const entries = [
['name', 'Vaibhav Tiwari'],
['age', 30],
['country', 'India']
];
const vaibhavTiwari = Object.fromEntries(entries);
console.log(vaibhavTiwari);
/* Output:
{
name: 'Vaibhav Tiwari',
age: 30,
country: 'India'
}
*/
7. Object.hasOwnProperty()
Check if an object has a specific property (own property) or it is inherited from the prototype chain.
const vaibhavTiwari = {
name: 'Vaibhav Tiwari',
age: 30,
country: 'India'
};
console.log(vaibhavTiwari.hasOwnProperty('name')); // Output: true
console.log(vaibhavTiwari.hasOwnProperty('gender')); // Output: false
8. Object.is()
Compare two values to determine if they are the same (similar to strict equality ===).
console.log(Object.is(5, 5)); // Output: true
console.log(Object.is('hello', 'hello')); // Output: true
console.log(Object.is(0, -0)); // Output: false
9. Object.seal()
Seal an object to prevent adding or removing properties, but allow changing existing properties.
const vaibhavTiwari = {
name: 'Vaibhav Tiwari',
age: 30,
country: 'India'
};
Object.seal(vaibhavTiwari);
vaibhavTiwari.age = 35; // This is allowed as the object is sealed.
vaibhavTiwari.gender = 'Male'; // This will have no effect as the object is sealed.
console.log(vaibhavTiwari); // Output: { name: 'Vaibhav Tiwari', age: 35, country: 'India' }
10. Object.getPrototypeOf()
Get the prototype of an object (the value of its internal [[Prototype]] property).
const person = { name: 'John Doe' };
const prototype = Object.getPrototypeOf(person);
console.log(prototype); // Output: {}
Conclusion
JavaScript provides a rich set of built-in object methods that offer powerful functionalities for working with objects. By mastering these methods, you can manipulate objects efficiently and handle complex data structures with ease in your JavaScript applications.
Comments
Post a Comment