I have a problem with making my function smarter. I achieve good results, but I think about the other solution.
I can take boolean
or object
as parameter. When the parameter is the object
I’d like to keep it as now. When the parameter is a boolean
value, I’d like to create an object, where keys are taken from another variable (less important) and values are this boolean
.
My solution assumes
- adding an empty items object at the beginning
- reading what is a value type and setting items according to the conditions
- taking items and converting to an array
- saving the array in state
Is that OK?
Check my code
const onAddAll = value => {
let items = {}
if (typeof value === 'boolean') {
terms.forEach(term => items[ term ] = value)
}
if (typeof value === 'object') {
items = value
}
const checked = Object.keys(items).filter(k => items[ k ])
setChecked(checked)
}
Imo, I wouldn’t need the second condition and make it smarter, bot no idea how. Please help!