0

I am getting an error when trying to parse and stringify some JSON data.

On this line:

this.copyOfColumns = JSON.parse(JSON.stringify(Object.assign([], this.columns)));

Here is the entire @Input (using Angular 4):

  @Input()
  set gridColumns(gridColumnsArr: Array<object>) {
    console.log('gridColumnsArr');
    console.log(gridColumnsArr);
    this.columns = this.sortActiveAndInactiveColumns(gridColumnsArr);
    console.log('this.columns');
    console.log(this.columns);
    this.copyOfColumns = JSON.parse(JSON.stringify(Object.assign([], this.columns)));
    console.log('this.copyOfColumns');
    console.log(this.copyOfColumns);
  }

Here is the data logged to the console (this.columns)...and the error(s) following:

enter image description here

2
  • why did you not set this.copyOfColumns = JSON.stringify(Object.assign([], this.columns)); Commented Apr 15, 2019 at 14:01
  • Tried that, get the exact same error Commented Apr 15, 2019 at 14:32

1 Answer 1

1

I assume you want to deep copy the array by using JSON.parse(JSON.stringify()). Apparently your data structure has circular references which fails the JSON.stringify().

Either you should sanitize your data to not contain ciruclar references or you could try using a library like flatted

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.