मान लें कि निम्नलिखित हमारा सेट है -
var name = new Set(['John', 'David', 'Bob', 'Mike']);
सेट को ऑब्जेक्ट में बदलने के लिए, JavaScript में Object.assign() का उपयोग करें -
var setToObject = Object.assign({}, ...Array.from(name, value => ({ [value]: 'not assigned' }))); उदाहरण
निम्नलिखित कोड है -
var name = new Set(['John', 'David', 'Bob', 'Mike']);
var setToObject = Object.assign({}, ...Array.from(name, value => ({ [value]: 'not assigned' })));
console.log("The Set result=");
console.log(name);
console.log("The Object result=");
console.log(setToObject); उपरोक्त प्रोग्राम को चलाने के लिए, आपको निम्न कमांड का उपयोग करने की आवश्यकता है -
node fileName.js.
यहाँ, मेरी फ़ाइल का नाम है demo260.js.
आउटपुट
यह कंसोल पर निम्न आउटपुट उत्पन्न करेगा -
PS C:\Users\Amit\javascript-code> node demo260.js
The Set result=
Set { 'John', 'David', 'Bob', 'Mike' }
The Object result=
{
John: 'not assigned',
David: 'not assigned',
Bob: 'not assigned',
Mike: 'not assigned'
}