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

90
Visualizações
How to assign the value to the element of an array of the object and return the updated object typscript

I am trying to return the updated values of Current for 'detailsState'. I tried as below:

  export function GetDetailsPayload(payload: INFO, detailsState: DetailsViewmodel, isSaved: boolean) {
  if (payload && payload.SSN && detailsState && detailsState.INFODetail && detailsState.INFODetail.Active) {

    detailsState.INFODetail.Active = payload;
    if (isSaved) {
      detailsState.INFODetail.Current.map((c, i) => {
        if (c.Id == detailsState.INFODetail.Active.Id) {
          c = payload;
          return c;
        }

      });

    }
  }
  return detailsState;
}

Actual result: detailsState.INFODetail.Current is same instead of payload.

Expected result: detailsState.INFODetail.Current having the value of payload when condition is met for one of the element in an array.

Thank you!

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

0

This part of your code:

detailsState.INFODetail.Current.map((c, i) => {
    if (c.Id == detailsState.INFODetail.Active.Id) {
        c = payload;
        return c;
    }
});

You map over the items in .Current, but you don't actually do anything with the mapped result. You probably meant to do this instead:

detailsState.INFODetail.Current = detailsState.INFODetail.Current.map((c, i) => {
    if (c.Id == detailsState.INFODetail.Active.Id) {
        c = payload;
        return c;
    }
});

Mind that that will replace certain elements with undefined when you don't return c within the mapper. You probably meant to return the original value otherwise:

detailsState.INFODetail.Current = detailsState.INFODetail.Current
    .map((c, i) => (c.Id == detailsState.INFODetail.Active.Id) ? payload : c);
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