I have this object:
const memorized = [
{
verses: [1, 2, 3, 4, 5],
when: '2022 Jan 14'
},
{
verses: [6, 7, 8, 9]
when: '2022 Jan 15'
},
{
verses: [10, 11, 12, 13, 14, 15]
when: '2022 Jan 16'
},
...
]
and how can I get the total verses like this:
const total_verses = [1, 2, 3, 4, 5, ..., 15]
My possible solution would be:
let total_verses = []
memorized.map(ar => total_verses.push(...ar.verses))
But I would like to do it by just filtering or mapping.
No need to push when you map - that is wasting an array
EITHER push in a forEach or just flatMap the array
const memorized = [ { verses: [1, 2, 3, 4, 5], when: '2022 Jan 14' }, { verses: [6, 7, 8, 9], when: '2022 Jan 15' }, { verses: [10, 11, 12, 13, 14, 15], when: '2022 Jan 16' } ];
const total_verses = memorized.flatMap(({ verses }) => verses)
console.log(total_verses);