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

96
Vistas
redux react update array after copying array

I have a question I'm facing a problem that I can't update array after copying it. If I copy an array and don't update the id, when I type something on input the text will appear in the same way where I copy it.

here is my Initialstate

const initialState = [
  {
    id: random numbers,
    options: [{ id: random numbers , value: '' },
    ],
  },
];

it will have a lot of option and I just would like to update options id

this is what i tried

    case COPY_QUESTION: {
      const newArray = [...state];
      const copyQuestion = newArray[action.payload];
        copyQuestion.options.map((option) =>
          Object.assign({}, option, {
            id: random number,
          }),
        );
      
      return [...state, copyQuestion];
    }

thanks for reading my question.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

it's caused due to call-by-reference. As I can see in your code, You are copying the reference of an array which might have the reason to overwrite details of the original array when you are typing. You can copy the values of the original array by using Javascript Object Prototype

so in that, you need to destruct your array or break your reference in the duplicate array. example

let A = [a,b,c]  //original Array
let B =  JSON.parse(JSON.strigify(A))  // duplicate Array
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