简体   繁体   中英

What would be the one-liner for an array with array of previous elements?

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.

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