following functions reduces an array and maps a new object.
getProps() {
return this.items
.reduce((map,props)=>
({...map, [props.name]: props.value, checked: true)})
}), {})
}
What I want to achieve is that I want to conditionally add the checked property(key) if a condition is satisfied.
something like this.
getProps() {
getProps() {
return this.items
.reduce((map, props)=>
( {...map
, [props.name]: props.value
, (props.required) ? checked: true : null
// which means that if props.required is true
// then add checked: true
// else do nothing (do not add checked)
,)})}), {})
}
EXP
what this means is that if props.required is true then add the property checked with value true (so that key:value pair checked:true will be added else do nothing (means do not add the key:value pair checked:true)
UPDATE
It is not about conditionally assign 'true' value to the checked property.
It is about to conditionally add checked property. Note the difference here...
checked: props.required ? true : falseresult.checked = this.items.some(props => props.required);on the end.