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

125
Views
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 answers
Answer question

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 Report

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 Report

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 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!