I want to find all contiguous sub-array from a given array.
For example, given array [1,2,3] I want to extract all contiguous sub-arrays:
[[1],[2],[3],[1,2],[1,2,3],[2,3]]
in any order.
I've attempted at a solution here: https://codepen.io/loganlee/pen/XWbPKeR?editors=0011
let a = [1, 2, 3];
let store = [];
a.forEach(
(n, i) => {
store.push([n]);
let nested = [n];
for (let index = i + 1; index < a.length; index++) {
nested.push(a[index]);
store.push(nested);
}
}
);
console.log(store);
I get log of store here:
[[1],[1,2,3],[circular object Array],[2],[2,3],[3]]
When I expect
[[1],[1,2],[1,2,3],[2],[2,3],[3]]
I'm not sure why "circular object Array" is present, and why [1,2] is missing.
Thank you very much!