Is it possible to dynamically set key name to spread operator?
For example I have:
'first, second, third'.split(',');
// Array(3) : [ 'first', 'second', 'third' ]
I want to have an object like this
{ 'first': 'first', 'second': 'second', 'third': 'third' }
By doing this right now I get:
{ ...'first, second, third'.split(',') };
// { 1: 'first', 2: 'second', 3: 'third' }
Can I dynamically set it or I have to iterate through and do it manually at this point?
I've ended up combine the two answers to use this:
const toObject = str => Object.assign(...str.split(/\s*,\s*/).map(key => ({ [key]: key })));