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

121
Vistas
Javascript - Adding new element to array using for loop

I'm trying to convert url. Using for loop to extract Acura, Audi. Here what I got so far:

var newSrpParams = 'year=2020-2022&make=Acura&make=Audi&model=A3&model=A5&trim=2.0T%20Premium&trim=2.0T%20S%20line%20Premium&normalBodyStyle=Hatchback&normalBodyStyle=Sedan&odometer=13000-38000&internetPrice=20000-50000';

const newSrpParamsArray = newSrpParams.split("&");

var oldSrpParams;

var makes = [];

for(var i = 0 ; i < newSrpParamsArray.length; i++){
  if(newSrpParamsArray[i].includes('make')) {
    const make = newSrpParamsArray[i].replace('make=','')
    makes.push(make);
    console.log(makes)
  }
};

The result is

[ 'Acura' ]
[ 'Acura', 'Audi' ]

As you see it has one more array. Is there a way to get only [ 'Acura', 'Audi' ]?

about 4 years ago · Santiago Gelvez
3 Respuestas
Responde la pregunta

0

That is happening because you are logging the array inside the for loop. If you move it outside you will get

['Acura', 'Audi']

The Code:

var newSrpParams = 'year=2020-2022&make=Acura&make=Audi&model=A3&model=A5&trim=2.0T%20Premium&trim=2.0T%20S%20line%20Premium&normalBodyStyle=Hatchback&normalBodyStyle=Sedan&odometer=13000-38000&internetPrice=20000-50000';

const newSrpParamsArray = newSrpParams.split("&");
console.log(newSrpParamsArray)

var oldSrpParams;

var makes = [];

for(var i = 0 ; i < newSrpParamsArray.length; i++){
  if(newSrpParamsArray[i].includes('make')) {
    const make = newSrpParamsArray[i].replace('make=','')
    console.log(make)
    makes.push(make);
  }
};

console.log(makes) // The change
about 4 years ago · Santiago Gelvez Denunciar

0

You were consoling the results inside the if statement it will run two times. So as a result make[] array print two times. That's why you get the two arrays.

var newSrpParams = 'year=2020-2022&make=Acura&make=Audi&model=A3&model=A5&trim=2.0T%20Premium&trim=2.0T%20S%20line%20Premium&normalBodyStyle=Hatchback&normalBodyStyle=Sedan&odometer=13000-38000&internetPrice=20000-50000';

const newSrpParamsArray = newSrpParams.split("&");

var oldSrpParams;

var makes = [];

for(var i = 0 ; i < newSrpParamsArray.length; i++){
  if(newSrpParamsArray[i].includes('make')) {
    const make = newSrpParamsArray[i].replace('make=','')
    makes.push(make);
  }
};

console.log(makes)

Make sure to console make[] from outside of the for a loop. That's only. I couldn't see any other wrong line in your code.

about 4 years ago · Santiago Gelvez Denunciar

0

FYI there's a native solution for getting values a from query string, check URLSearchParams

var newSrpParams = 'year=2020-2022&make=Acura&make=Audi&model=A3&model=A5&trim=2.0T%20Premium&trim=2.0T%20S%20line%20Premium&normalBodyStyle=Hatchback&normalBodyStyle=Sedan&odometer=13000-38000&internetPrice=20000-50000';

const makes = new URLSearchParams(newSrpParams).getAll('make');

console.log(makes);

about 4 years ago · Santiago Gelvez 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