I'm wondering if it's possible to achieve the below in a cleaner/more efficient way?
const data = [
{ active: false },
{ active: false },
{ active: true },
{ active: false },
{ active: true },
]
const split = (data) => {
const activeData = data.filter(({active}) => active).map(element => 1)
const inactiveData = data.filter(({active}) => !active).map(element => 0)
return [
activeData,
inactiveData,
]
}
console.log(split(data))
The problem here, I think, is that this approach is not efficient, because it's filtering and mapping through the same array twice. Is there a way to do it just once?