<style> .reveal { font-size: 32px; } </style> ### Enhancing Node.js Skills ## JavaScript Object Methods ![image](https://hackmd.io/_uploads/HJAvq6pHp.png) --- ## Introduction to Object Methods - Essential part of JavaScript programming. - Used extensively in Node.js and web development. - Enhance code readability and efficiency. --- ## What are Object Methods? - Functions associated with an object. - Allow you to manipulate, access, and manage data within objects. - Examples: `Object.keys()`, `Object.values()`, `Object.entries()` --- ## Why are They Important? * **Enhanced Code Structure and Readability:** Object methods encourage a more structured approach to coding. By encapsulating functionality within objects, code becomes easier to read and understand. --- * **Improved Data Handling and Manipulation:** JavaScript objects are often used to represent data models. Object methods provide efficient ways to manipulate this data, such as adding, modifying, or deleting properties. --- * **Encouragement of the DRY Principle (Don't Repeat Yourself):** Object methods can be reused across different parts of an application. This avoids the need for repetitive code. --- * **Performance Optimization:** Efficient use of object methods can significantly impact the performance of a web application. --- ## Today's Focus - Introduce a few key object methods. - Understand their usage with simple examples. - Learn best practices in a Node.js context. --- ## Method 1: `Object.keys()` - Retrieves all the keys from a JavaScript object. - Returns an array of strings representing the property names. **Example Code:** ```javascript const person = { name: 'Alice', age: 25, job: 'Engineer' }; const keys = Object.keys(person); console.log(keys); ``` --- ```text ['name', 'age', 'job'] ``` --- ## `Object.keys()` #### Scenario: Filtering User Data Imagine you have an object representing user data, and you need to create a list of all the fields (keys) that have non-empty values. This scenario is common in form data processing or data validation. --- ```javascript const userData = { name: "John Doe", email: "johndoe@example.com", age: 30, address: "", phoneNumber: "123-456-7890" }; ``` #### Filtering non-empty fields: ```javascript const nonEmptyFields = Object.keys(userData) .filter(key => userData[key] !== ""); console.log(nonEmptyFields); ``` --- ```text ['name', 'email', 'age', 'phoneNumber'] ``` --- ## Method 2: `Object.values()` - Complements `Object.keys()` by retrieving all the values. - Returns an array containing the values of each property in the object. **Example Code:** ```javascript const person = { name: 'Alice', age: 25, job: 'Engineer' }; const values = Object.values(person); console.log(values); ``` --- ```text ['Alice', 25, 'Engineer'] ``` --- ## Method 3: `Object.entries()` - Returns an array of key-value pairs. - Useful for iterating over objects. **Example Code:** ```javascript const person = { name: 'Alice', age: 25, job: 'Engineer' }; const entries = Object.entries(person); console.log(entries); ``` --- ```text [['name', 'Alice'], ['age', 25], ['job', 'Engineer']] ``` --- ## Method 4: `Object.assign()` - Used to copy values from one or more source objects to a target object. - Demonstrates shallow copying. --- **Example Code:** ```javascript const person = { name: 'Alice', age: 25, job: 'Engineer' }; const contactDetails = { email: 'alice@example.com', phone: '123-456-7890' }; const combinedDetails = Object.assign({}, person, contactDetails); console.log(combinedDetails); ``` --- ```text { name: 'Alice', age: 25, job: 'Engineer', email: 'alice@example.com', phone: '123-456-7890' } ``` --- ## Method 5: `Object.freeze()` - Makes an object immutable, preventing new properties from being added. - Existing properties cannot be removed or altered. **Example Code:** ```javascript const frozenPerson = Object.freeze(person); frozenPerson.age = 30; console.log(frozenPerson); ``` --- ```text { name: 'Alice', age: 25, job: 'Engineer' } ``` ---
{"title":"Introduction to object methods","description":"Web Development Bootcamp","contributors":"[{\"id\":\"c5c54e30-9692-49dc-baf9-f45297507939\",\"add\":4792,\"del\":592}]"}
    85 views
   Owned this note