Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

157
Views
Modifying deeply nested object inside of a BehaviorSubject array

I am building an app in angular. My service DocumentService contains a BehaviorSubject of an array of Documents.

documents: BehaviorSubject<Document[]>

Here are the defintions of the classes I have :

class Document {
  name: string
  files: File[];
}

class File {
  pages: Page[];
}

class Page {
  data: any;
  scale: number;
}

My question is, what is the best way to update the scale property in a page? For this I would have to go down into the document, into the file, and then into the page to modify it. However, I cannot modify it in place since I have to use this.documents.next(newDocumentsArray). Problem is, deep copying doesn't work - JSON.parse(JSON.stringify(...)) gives me a circular structure error, while the other ways (.map, Object.assign, ...) only give me shallow copies that then modify the page in place, which is something that we can't do.

Any tips? Thanks

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you wish to keep your current deep structure, you'd need to drill down:

const newDocs = oldDocs.map(doc => {
  if (doc !== docToChange) {
    return doc;
  } else {
    return {
      ...doc,
      files: doc.files.map(file => ({
        ...file,
        pages: file.pages.map(page => {
          // do page change here
        })
      }))
    };
  }
});

I'd suggest a switch to a flat Entity-like structure, where all Documents, Files, and Pages are stored by unique ID in separate dictionaries, and a Document's files array contains the ids of its Files, with the same for Files referring to their Pages. Does that make sense?

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!