Foreach with structuredClone run into RangeError

90 views Asked by At
const result: Model[] = [];
array.forEach((element) => {
  if (condition1)
    result.push(structuredClone(element));

  if (condition2)
    result.push(structuredClone(element));
});

The object is nested:

export interface Model extends Dto {
  indentation?: number;
  previous?: Model;
  next?: Model;
}

export interface Dto {
  ...
  children?: Array<Dto>;
  contentChunks?: Array<ContentChunk>;
  id?: number;
  ...
}

If the size of array is small i works. But on big arrays it runs into

ERROR RangeError: Maximum call stack size exceeded

Suggestions to improve appreciated.

Update:

on changing structuredClone to JSON.parse(JSON.stringify

ERROR TypeError: Converting circular structure to JSON
--> starting at object with constructor 'Object'
|     property 'next' -> object with constructor 'Object'
--- property 'previous' closes the circle
0

There are 0 answers