I need to split an object array into two variables. The first variable (main) should get the object with a title.
The second one (content) should get all other objects.
Sample data
[
{ _id: '1', title: 'Main' },
{ _id: '2', foo: 'bar' },
{ _id: '2', foo: 'bar' }
]
I did it withfind()/filter() commands, but is it really necessary to find twice?
const main = data.find(doc => doc.title)
const content = data.filter(doc => !doc.title)
Is it possible to extract the main object instead of finding it?
data.filteron the second line, I don't think there is anything wrong with it. Unless your array is huge there won't be any performance difference. And your current solution is much more readable than some other approaches.let {false: docs, true: [main]} = _.groupBy(ary, x => !!x.title)