I have [1,2,3,4,5,6,7,8,9]
I need
[
[1],
[1,2],
[1,2,3],
[1,2,3,4],
[1,2,3,4,5],
...
]
I imagine the result something like const var = array.reduce **black magic**
I tried something with reduce and a concat inside but I keep getting 'circular object Array'
reduce is not always the answer. Sometimes there are better choices, eg map
and slice
:
array.map((_, i) => array.slice(0, i + 1))
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.