Science Knowings: JavaScript Course For Social Media

Object.fromEntries() Method

Object.entries() Recap

Recall how Object.entries() converts an object's key-value pairs into an array of arrays.

const obj = { name: 'Alice', age: 30 };
const entries = Object.entries(obj);

Introducing Object.fromEntries()

Enter Object.fromEntries(), its complementary counterpart! It reverses the process, transforming an array of key-value pairs back into an object.

const newObj = Object.fromEntries(entries);

Power and Convenience

  • Simplifies object creation from data structures like arrays and maps.
  • Enhances flexibility in data manipulation tasks.

Effortless Key-Value to Object Transformation

const keyValueArray = [['name', 'Alice'], ['age', 30]]; 
const newObj = Object.fromEntries(keyValueArray);

Effortlessly convert key-value pairs into objects without manual construction.

Arrays and Maps to Objects

const arr = [['a', 1], ['b', 2]]; 
const objFromArray = Object.fromEntries(arr);

const map = new Map([['c', 3], ['d', 4]]);
const objFromMap = Object.fromEntries(map);

Easily transform arrays and maps containing key-value pairs into objects.

Data Wrangling Applications

const flattenedData = [['id', 1], ['name', 'Alice'], ['age', 30]]; 
const userObj = Object.fromEntries(flattenedData);

Convert flattened data into objects for easy manipulation and analysis.

Real-World Use Cases

  • Object Restructuring: Reshape objects with different key names or nested structures.

    const originalObj = { name: { first: 'Alice', last: 'Smith' } }; 
    const restructuredObj = Object.fromEntries(Object.entries(originalObj) .map(([key, val]) => [key.replace('.', '_'), val]));

  • Query String Parsing: Convert query strings (e.g., ?name=Alice&age=30) into objects.

Advanced Applications

  • Custom Object Creation: Flexibly construct objects with custom properties and values based on specific criteria.

    const filterCriteria = ['name', 'age']; 
    const filteredObj = Object.fromEntries(Object.entries(originalObj) .filter(([key]) => filterCriteria.includes(key)));

  • Array of Objects to Single Object: Consolidate multiple similar objects into a single object with key-value pairs merged.

    const users = [{ name: 'Alice', age: 30 }, { name: 'Bob', age: 25 }]; 
    const mergedObj = Object.fromEntries( => Object.entries(user)));

Complementing Object.entries()

Object.fromEntries() and Object.entries() are complementary methods that enable comprehensive object manipulation:

  • Object.entries(): Extract key-value pairs from objects.
  • Object.fromEntries(): Reconstruct objects from key-value pairs.

Object.getOwnPropertyDescriptors() Method

Why Learn This?
This method provides deep insights into an object's properties, their attributes, and their relationships with the prototype chain. It becomes essential for advanced object manipulation, debugging, and understanding inheritance.

Follow Us!
Stay tuned for our next session on Object.getOwnPropertyDescriptors() method. We'll unlock the power of exploring object properties like never before.