My data is following
data=[
{
id: 1,
name: 'John Doe',
contacts: '[email protected]',
accepted: true
},
{
id: 2,
name: 'Jane Doe',
contacts: '[email protected]',
accepted: false
},
]
I want to filter (if filterKey variable is not empty/null) and sort(if sortKey variable is not empty/null). I can do it with some if/then but is there a better way?
let computedList = data
if(filterKey) {
computedList = data.filter(item => item.name.includes(filterKey))
}
if(sortKey) {
computedList = data.sort(...)
}
computedList.map(item => <div>item.name</div>)
filterKeyandsortKeycome from?filterKeyandsortKeyare present, it always sorts thedatanot the result of the filter.computedListin sortKey block.filterKeyandsortKeycome externally (from React props).