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

248
Vistas
React: unrequested variable appended in Array

fresh learner in react here...I'm trying to update a set of data fetched via an API with a custom information (it's a ticketmaster API website, i get the information on the show, when adding to the basket i would like to add to that set of data the number of tickets selected). The idea was to move "number" inside "items"

  const [cart, setCart] = useState({
    display: "none",
    items: [],
    number: 0,
  })

when i click to ad to cart button

setCart({ ...cart, items: [...cart.items, data,data["ticketInCart"] = counter], number:counter})

...as i want to keep the old information, (hence the spread operator ...cart), keep what i have in cart.items, add a new element (data), then add "ticketInCart" to data...

this works, and cart.items will have new informations as well as a variable named ticketInCart with the right information, but when i do console.log strangely i also find a "1: 1" and a length of 2 at the end of the array (where the first number is the index, the second number is the ticketInCart value)

Object { items: (2) […], number: 1, display: "block" }
display: "block"
items: Array [ {…}, 1 ]
0: Object { name: "Anastasia (Touring)", type: "event", id: "k7vGF4MnED7Au", … }
1: 1
length: 2
<prototype>: Array []
number: 1
<prototype>: Object { … }

how do i add a variable to the object avoiding the creation of another element each time i do so?

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