Transform array into object with true values

Let's assume that you have the following array:

let tags = ['React', 'JavaScript', 'Firebase'];

To transform the array into an object assigning true values:

// The comma operator evaluates from left to right:
// 1. Assigns 'true' to the keys
// 2. Returs the accumulator
let obj = tags.reduce((acc, tag) => (acc[tag] = true, acc), {});

// {React: true, JavaScript: true, Firebase: true}

The comma operator is useful to evaluate functions from left to right, returning the value of the last function.

Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.