- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert set to object - JavaScript?
The task we are going to perform in this article, is “Convert set to object”. Before we jump into the article, let’s have quick view on the sets and objects in JavaScript.
A collection of distinctive values makes up a JavaScript Set. In a Set, a value can only appear once. Any value of any data type can be stored in a set.
An object in JavaScript is a separate entity having properties and a type. Consider comparing it to a cup. A cup is an item with characteristics. A cup has a design, weight, color, material, and other characteristics. In a similar manner, JavaScript objects can have properties that specify their attributes.
Let’s dive into the article to learn more about converting set to object in JavaScript. To convert the set to object, use the Object.assign() in JavaScript.
Object.assign() in JavaScript
Using one or more source objects as sources, the Object.assign() method copies all enumerable own properties to a target object. It gives back the altered target object.
Syntax
Following is the syntax for Object.assign()
Object.assign(target, ...sources)
Let’s consider the following examples to understand more about converting set to object.
Example
In the following example, we are using Array.form as a mapping function to convert values in a set to objects, and then we use Object.assign() with spread syntax to merge the array of objects into a single result.
<!DOCTYPE html> <html> <body> <p id="tutorial"></p> <script> const uom = new Set(['BMW', 'BENZ', 'AUDI', 'DUCATI']); const res = Object.assign(...Array.from(uom, v => ({[v]:''}))); document.getElementById("tutorial").innerHTML=JSON.stringify(res); </script> </body> </html>
When the script gets executed, it will generate an output consisting of an object printed on the webpage that was caused by the event that was triggered when the user executed the script.
Example
Considering the following example, where we are using Array.reduce to get the elements from the set and send them into an object.
<!DOCTYPE html> <html> <body> <p id="tutorial"></p> <script> const tutorial = new Set(['RC', 'RX100', 'DUKE', 'VESPA']); const getObjectFromSet = (set) => { return [...set].reduce((x, y) => {x[y] = ""; return x; },{}); } document.getElementById("tutorial").innerHTML=JSON.stringify(getObjectFromSet(tutorial)); </script> </body> </html>
On running the above script, the output window will pop up, triggering the event and displaying the object on the webpage that was obtained from the set used in the above script.
Example
Let’s consider the other example where we are using Object.fromEntries, which takes all the key-value pairs from a set and returns them in the form of an object.
<!DOCTYPE html> <html> <body> <p id="tutorial"></p> <script> const tutorial = new Set(['Avatar', 'RRR', 'Bheem', 'Dheera']); const getObjectFromSet = (set) => { return Object.fromEntries(Array.from(set, (a) => [a, ""])); } document.getElementById("tutorial").innerHTML=JSON.stringify(getObjectFromSet(tutorial)); </script> </body> </html>
When the script gets executed, it will generate an output consisting of an object printed on the webpage, which was caused by the conversion of the set when the event got triggered.
- Related Articles
- Convert object to a Map - JavaScript
- Set JavaScript object values to null?
- Convert a string to hierarchical object - JavaScript
- How to convert array to object in JavaScript
- How to convert a string to JavaScript object?
- How to convert Array to Set in JavaScript?
- How to set JavaScript object values dynamically?
- Convert object of objects to array in JavaScript
- Convert object to array of objects in JavaScript
- How to convert JSON text to JavaScript JSON object?
- How to convert square bracket object keys into nested object in JavaScript?
- Python Pandas - Convert PeriodIndex object to Timestamp and set the frequency
- Strip quotes with JavaScript to convert into JSON object?
- How to convert a JavaScript date object to a string?
- JavaScript - Set object key by variable
