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

116
Visualizações
insert/update object in array using index when array is empty in redux react

I am working on a react project and using redux for state management.I want to update values in an array using index but initially the array is empty.

How can I do it?

I am doing this:

case Actions.UPDATE:
      return {
        ...state,
        defaultCodeList: state.defaultCodeList.map((code, index) => (index === action.index ? {
          code_sys_id: state.masterName, code_sys_cat_id: state.codeSystem, [`${state.codeSystem}_description`]: action.description,
        } : codeset)),
      };

Initailly defaultCodeList:[] and on pressing of a button I am triggering this action. I want to put the above object on that particular index when the index matches.

Any leads on this?

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

0

Create a new array set the index of the array identical to the action's index to the object value. This would set all other indexes to undefined. ou then can now set the undefined values in the array to codeset when assigning it to defaultCodeList

case Actions.UPDATE:
    const newDefaultCodeList = [...state.defaultCodeList];
    newDefaultCodeList[action.index] = {
        code_sys_id: state.masterName, code_sys_cat_id: state.codeSystem, [`${state.codeSystem}_description`]: action.description,
    } // this sets the remaining values at other indexes to undefined
    return {
        ...state,
        defaultCodeList: newDefaultCodeList.map((code, index) => (index !== action.index && codeset)),
    };
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