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

159
Vistas
How to modify object value stored in a literal through this context in Vue or JavaScript?

I am in Vue.js and trying to correct inappropriate quantity input value like "12jd". They belong to item objects which are in a array literal called store (which is defined in data( ) return). The problematic code is in the input event handler function which gets the item's index - idx in which the bad quantity was written.

Everything goes well until I try to assign the proper array, with no bad quantity, to the original store in the this context. This part does not work, no value gets changed in the original.

failed(idx) {

  const storeCopy = lodash.cloneDeep(this.store); //deep copy the store literal

  const unmodified = storeCopy.filter((item) => item.id !== idx);

  const modified = storeCopy.filter((item) => item.id === idx)[0]; //slice out the relevant part
  const modifiedCopy = JSON.parse(JSON.stringify(modified)); //deep copy the item object

  const input = parseInt((" " + modifiedCopy.quantity).slice(1)); //get parsed input from deep copied string

  const store = [
    Object.defineProperty(modifiedCopy, "quantity", {
      value: isNaN(input) ? 0 : input,
    }),
    ...unmodified,
  ]; //correct the input and put it back to the array
  console.log(store);

  this.store.length = 0; //from this it's messed up
  this.store = store;
}

I do deep copy even three times, still the problem backpropagates to console logged array after uncommenting the last two lines. Without them it's fine.

Why does the reference not get garbage collected, after setting this.store.length to 0, or this.store to [] at all? What do I miss out, is there any original reference left in the new array maybe? Any help appreciated! Thanks

Edited

Here's the output without then with the two last line:

Without the last two lines

I don't even understand who it's possible that store in the local scope is affected backwards, by inserting those two lines. Anyways It gets restored somehow to the bad value "12jd".

With the two lines

Even the log looks different, I have to click on the three dots to reveal the values.

about 4 years ago · Juan Pablo Isaza
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