How to use Array.from() and map() methods In Javascript
The Array.from() method can be used with Object.entries() to convert an object’s key-value pairs into an iterable array. The map() method transforms each entry into an object, creating an array of objects representing the original object’s keys and values.
Syntax:
Array.from(Object.entries(obj)).map();
Example: The below code uses above-discussed methods to create an array of objects from another object.
const myObject = {
key1: "HTML",
key2: "CSS",
key3: "JavaScript",
};
const arrayOfObjects = Array.from(
Object.entries(myObject)
).map(([key, value]) => ({ [key]: value }));
console.log(arrayOfObjects);
Output
[ { key1: 'HTML' }, { key2: 'CSS' }, { key3: 'JavaScript' } ]
How to Create Array of Objects From Keys & Values of Another Object in JavaScript ?
Creating an array of objects from the keys and values of another object involves transforming the key-value pairs of the original object into individual objects within an array. Each object in the array represents a key-value pair from the source object.