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

115
Visualizações
adding js variable into url to add the products

EDIT I found the solution but this time I can not remove the comma from the end of the url. Below is my entire code. I just need to remove the from end of the url. i tried if statement but i failed.

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);

EDIT END

I am trying to add js variable in the url to add the products which user chose from a page.

that.productsSelected is array of javascript objects which I am getting from the page. Then to see the values I am using for loop, for now I am using console.log just for checking, see below.

//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>";
}

and I am getting values which is absolutely correct,
id 7464
quantity 1

Now my requirement is the value of id and quantity I want to add in the url for my online store.

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

please note the product can be 2 or 8 or whatever it depends on user.

Can someone please help me? I am trying to get this solution for 1 week but miserably failed everytime

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

0

Use map() and 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 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