I'm trying to have a configurable checkbox and want to have my array's values to be the keys for the object.
Given:
let colors = [red, blue, black, yellow, orange]
How do I make it so that it becomes:
colorChecklist = {
red: true,
blue: true,
black: true,
yellow: true,
orange: true
}
You can use reduce here .
One-liner
const colorChecklist = colors.reduce((acc, curr) => (acc[curr] = true, acc), {});
let colors = ["red", "blue", "black", "yellow", "orange"];
const colorChecklist = colors.reduce((acc, curr) => {
acc[curr] = true;
return acc;
}, {});
console.log(colorChecklist);
Just use Array.prototype.reduce
const [red, blue, black, yellow, orange] = ["red", "blue", "black", "yellow", "orange"]
let colors = [red, blue, black, yellow, orange];
let colorChecklist = colors.reduce((acc, key) => (acc[key] = true, acc), {});
console.log(colorChecklist);