Convert javascript array to object of same keys/values

Object.assign(
  {},
  ...['value1', 'value2', 'value3', 'value4', 'value5'].map((value) => ({
    [value]: value,
  })),
)

returns

{value1: "value1", value2: "value2", value3: "value3", value4: "value4", value5: "value5"}

No common ESlint rules error.


This can be done with a simple reduce call:

// Demo data
var source = ['someValue1', 'someValue2', 'someValue3', 'other4', 'other5'];


// This is the "conversion" part
var obj = source.reduce(function(o, val) { o[val] = val; return o; }, {});


// Demo output
document.write(JSON.stringify(obj));