convert set to object

后端 未结 4 1251
旧时难觅i
旧时难觅i 2021-01-20 20:07

I have a set which needs to be converted into an object with the set\'s unique values as the object keys and an empty string as each element\'s value in the object.

H

4条回答
  •  臣服心动
    2021-01-20 20:50

    You can use Array.from with a mapping function to convert each value inside your set into an object and then use Object.assign() with the spread syntax to merge the array of objects into a single resulting object like so:

    const uom = new Set(['inches', 'centimeters', 'yards', 'meters']);
    const res = Object.assign(...Array.from(uom, v => ({[v]:''})));
    console.log(res);

提交回复
热议问题