Object.keys()
to creating objects with Object.fromEntries()
, we provide code snippets and descriptions for each function. Enhance your JavaScript skills and understanding with our detailed guide.1. Extracting Keys with Object.keys()
The Object.keys()
method returns an array of a given object’s own property names. Here’s an example:
const person = { name: 'John', age: 30, job: 'Developer' };
const keys = Object.keys(person);
// Result: keys = ['name', 'age', 'job']
2. Extracting Values with Object.values()
The Object.values()
method returns an array of a given object’s own enumerable property values. Here’s how you can use it:
const person = { name: 'John', age: 30, job: 'Developer' };
const values = Object.values(person);
// Result: values = ['John', 30, 'Developer']
3. Extracting Key-Value Pairs with Object.entries()
The Object.entries()
method returns an array of a given object’s own enumerable property [key, value]
pairs. Here’s an example:
const person = { name: 'John', age: 30, job: 'Developer' };
const entries = Object.entries(person);
// Result: entries = [['name', 'John'], ['age', 30], ['job', 'Developer']]
4. Merging Objects with Object.assign()
The Object.assign()
method copies the values of all enumerable own properties from one or more source objects to a target object. Here’s how you can use it:
const target = { a: 1, b: 2 };
const source = { b: 3, c: 4 };
const mergedObject = Object.assign({}, target, source);
// Result: mergedObject = { a: 1, b: 3, c: 4 }
5. Freezing Objects with Object.freeze()
The Object.freeze()
method freezes an object, preventing new properties from being added and existing properties from being removed or modified. Here’s an example:
const frozenObject = Object.freeze({ name: 'Jane', age: 25 });
// Attempting to modify or add properties will result in an error
6. Sealing Objects with Object.seal()
The Object.seal()
method seals an object, preventing new properties from being added and marking all existing properties as non-configurable. Here’s how you can use it:
const sealedObject = Object.seal({ city: 'New York', population: 8000000 });
// Attempting to add or remove properties will result in an error
7. Creating Objects with Object.create()
The Object.create()
method creates a new object with the specified prototype object and properties. Here’s an example:
const personPrototype = { greet: function() { console.log('Hello!'); } };
const john = Object.create(personPrototype);
john.name = 'John';
// john now has the 'greet' method from the prototype
8. Checking Property Existence with Object.hasOwnProperty()
The Object.hasOwnProperty()
method returns a boolean indicating whether the object has the specified property as its own property (not inherited). Here’s how you can use it:
const car = { make: 'Toyota', model: 'Camry' };
const hasModel = car.hasOwnProperty('model');
// Result: hasModel = true
9. Iterating Over Object Properties with Object.keys().forEach()
Combining Object.keys()
with forEach()
allows you to iterate over the properties of an object. Here’s an example:
const person = { name: 'Alice', age: 28, job: 'Designer' };
Object.keys(person).forEach(key => {
console.log(`${key}: ${person[key]}`);
});
// Logs 'name: Alice', 'age: 28', 'job: Designer'
10. Creating Object from Key-Value Pairs with Object.fromEntries()
The Object.fromEntries()
method transforms a list of key-value pairs into an object. Here’s an example:
const entries = [['name', 'John'], ['age', 30], ['job', 'Developer']];
const person = Object.fromEntries(entries);
// Result: person = { name: 'John', age: 30, job: 'Developer' }
Comments
Post a Comment