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

154
Vistas
Pushing array into JS GET request using spread

I’m trying to pass an array to a php script as part of an Ajax call. Whilst POST might be simple, it seems GET is the right technique to do this, I’m also making the api code so it can be either way that I send to it. Therefore I want to put the array contents into the url as 2 separate parameters of the request.

After a bit of reading about loops I found the suggestion of using “spread”: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax

However I’m not clear how to use it properly. This was my code:

const arg = ['p0', 'p1'];
callMe(arg);

function callMe(…arg){
// ajax GET request
   let url = `api.php?p0=${1}&p1=${2}`;
   // rest of code…
   console.log(arg);
}

This still prints out as an array. How should I be using spread to get p0, p1 put into a url from an array?

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

0

You're actually using the rest syntax, which is how you let a function have an infinite number of arguments which will be presented as an array of arguments.

function foo(...args) {
   console.log(args);
}

foo('hello', 1, 'bar', true);

But what you're looking for is to use the spread syntax when passing the args to the callMe function.

See the example below. Here we expect two parameters p0 and p1. When using the spread syntax we pass each item in the array as an argument to the callMe function.

const args = ['foo', 'bar'];

function callMe(p0, p1){
   let url = `api.php?p0=${p0}&p1=${p1}`;
   console.log(url);
}

callMe(...args); // Same as callMe(args[0], args[1]);

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