Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

105
Views
agregando la variable js en la url para agregar los productos

EDITAR Encontré la solución pero esta vez no puedo quitar la coma del final de la url. A continuación se muestra mi código completo. Solo necesito eliminar el del final de la URL. Intenté si la declaración pero fallé.

 var param = ""; //for loop for retrieving the selected values from array of object for (let i = 0; i < that.productsSelected.length; i++) { //console.log(that.productsSelected[i].id); + "<br>"; //console.log(that.productsSelected[i].quantity); + "<br>"; param += that.productsSelected[i].id +"&quantity="+ that.productsSelected[i].quantity; if(!(i == that.productsSelected.length)){ param += ","; } } console.log(param);

EDITAR FIN

Estoy tratando de agregar la variable js en la URL para agregar los productos que el usuario eligió de una página.

that.productsSelected es una matriz de objetos javascript que obtengo de la página. Luego, para ver los valores que estoy usando para el ciclo, por ahora estoy usando console.log solo para verificar, vea a continuación.

 //for loop for retrieving the selected values from array of object for (let i = 0; i < that.productsSelected.length; i++) { console.log(that.productsSelected[i].id); + "<br>"; console.log(that.productsSelected[i].quantity); + "<br>"; }

y obtengo valores que son absolutamente correctos,
identificación 7464
Cantidad: 1

Ahora mi requisito es el valor de la identificación y la cantidad que quiero agregar en la URL de mi tienda en línea.

 window.location.href = "/cart/?add-to-cart=idvalue:"+quantity+",idvalue:"+quantity+",idvalue:"+quantity+",idvalue:"+quantity+",idvalue:"+quantity+"";

tenga en cuenta que el producto puede ser 2 u 8 o lo que sea que dependa del usuario.

¿Puede alguien por favor ayudarme? Estoy tratando de obtener esta solución durante 1 semana, pero fallé miserablemente cada vez.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Usa map() y join()

 let cart_params = that.productsSelected.map(({id, quantity}) => `${id}:${quantity}').join(','); window.location.href="/cart/?add-to-cart=" + cart_params;
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!