Would it be possible to dynamically assign key names to the spread operator?
For instance, consider the following:
'first, second, third'.split(',');
// Array(3) : [ 'first', 'second', 'third' ]
I would like to create an object like this
{ 'first': 'first', 'second': 'second', 'third': 'third' }
Currently, when attempting this, I receive:
{ ...'first, second, third'.split(',') };
// { 1: 'first', 2: 'second', 3: 'third' }
Is there a way to dynamically set this or must it be manually done through iteration?
I have managed to merge two answers together and use the following solution:
const toObject = str => Object.assign(...str.split(/\s*,\s*/).map(key => ({ [key]: key })));