Files
30-seconds-of-code/snippets/js/s/to-flags-object.md
Angelos Chalaris 9d032ce05e Rename js snippets
2023-05-19 20:23:47 +03:00

532 B

title, type, language, tags, author, cover, dateModified
title type language tags author cover dateModified
Array to flags object snippet javascript
array
object
chalarangelo glass-blowing 2022-04-12T05:00:00-04:00

Converts an array of strings into an object mapping to true.

  • Use Array.prototype.reduce() to convert the array into an object, where each array value is used as a key whose value is set to true.
const flags = arr => arr.reduce((acc, str) => ({...acc, [str]: true }), {});
flags(['red', 'green']); // { red: true, green: true }