I need to split an array of objects into two variables. The first variable ( main
) should get the object c title
. The second ( content
) should get all other objects.
Data examples
[
{ _id: '1', title: 'Main' },
{ _id: '2', foo: 'bar' },
{ _id: '2', foo: 'bar' }
]
I did this with the find()
/ commands filter()
, but do I really need 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 searching for it?
source
share