convert set to object

后端 未结 4 1249
旧时难觅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 21:00

    Create an object, then loop over the values in the set and create a property for each one. The reduce method is probably the most compact way to do that.

    const uom = new Set(['inches', 'centimeters', 'yards', 'meters']);
    const result = Array.from(uom).reduce( (a,c) => { a[c]=""; return a; }, {});
    console.log(result);

提交回复
热议问题