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

126
Visualizações
Editing object inside an object in an array of objects

I have the following two types:

export default interface ImageDataForAnnotation {
    src: string;
    name: string;
    key: number;
    regions?: Annotation[]; //This is defined below
    pixelSize?: PixelSize
}


export interface Annotation {
    type: string,
    x: number,
    y: number,
    w: number,
    h: number,
    highlighted: boolean,
    editingLabels: boolean,
    color: string,
    cls: string | undefined,
    id: number,
    image?: number,

}

Then i have an array where each image holds a number of annotations

  const [images, addImage] = useState<Array<ImageDataForAnnotation>>([]);
  
  //Definition of arary

  let imageArray = [...images];

I then want to edit an annotation in a given image, where i have both indexes to do something like: imageArray[image_index].regions![annotation_index].update(new_annotation)

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I would firstly rename the method as addImage is misleading because the method may be used to edit/update, or even remove/delete items from images array.

So, firstly:

 const [images, setImages] = useState<Array<ImageDataForAnnotation>>([]);

Next, let us assume the index are available at variables named: imageIndex and annotationIndex (again, CamelCase is my preference) and the value that needs to be used is at newAnnotation, then to update the corresponding item within the state-variable images, here's what I would do:

setImages(prev => {
  const curr = [...prev];
  curr?.[imageIndex]?.regions?.[annotationIndex] = newAnnotation;
  return curr;
});

Use the callback function to access the previous-state of images and then update the specific item and return the updated version. I've used a shallow-copy curr for better readability & it's not necessary.

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