What is a more compact way to convert color=["red", "green", "blue", "Other"] into colorObjFinal = { blue: true, green: true, Other: "Other", red: true }?
This is my long function, I would like to shorten it:
let color = ["red", "green", "blue", "Other"];
let colorObj;
let colorObjFinal = {};
console.log("color:", color);
colorObj = color.reduce((a, v) => ({
...a,
[v]: v
}), {});
console.log("colorObj:", colorObj)
for (var key in colorObj) {
if (colorObj.hasOwnProperty(key)) {
if(key == 'Other'){
colorObjFinal[key] = colorObj[key];
} else {
colorObjFinal[key] = true;
}
}
}
console.log("colorObjFinal:", colorObjFinal)