Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

159
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda