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();
  }

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.