2

I am having an issue while cloning objects. I have strategies array to which i am trying to add strategy objects.It works some times while errors with the following error message. Could somebody tell me what the problem could be. The strategy object consists of objects of objects. In the Add method , I am trying to add Strategy of element zero to the strategy array.

export interface Strategy  {

        domicile: Domicile;
        captiveAssumption: StrategyCaptiveAssumption;
        modelingAssumptions: StrategyModelingAssumption;
        selectedLinesOfBusiness: SelectedLineOfBusinessInput[];
        accountRules: StrategySpecialAccountRules;
        minCapitalContribution: StrategyMinCapitalContribution;
        results: Results;
    }

Converting circular structure to JSON at JSON.stringify ()

 add() {
    if (!this.showAddStrategy) {
      return;
    }

    const strategy: Strategy = JSON.parse(JSON.stringify(this.strategies[0]));
    this.strategies.push(this.strategies[0]);
    this.save.emit();
    this._expandLastStrategy();
  }
Tom
  • 8,175
  • 41
  • 136
  • 267

1 Answers1

2

A circular structure is a structure which references itself as a value. JSON.stringify does not support such structures, since it would result in an infinitely-long string.

What you need is a deep cloning function which does not use JSON.stringify. Such implementation can be found here.

Slavadir
  • 71
  • 2