简体   繁体   中英

How to destruct array of numbers in an object and combine to one in javascript?

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'
 },
...
]

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM