Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

158
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda