मान लीजिए निम्नलिखित हमारा Set है:
var name = new Set(['John', 'David', 'Bob', 'Mike']);
Set को ऑब्जेक्ट में बदलने के लिए, 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'
}