0

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' ]?

asked Apr 12, 2022 at 4:58
3
  • 2
    There is only one array, you just logged the same array twice at different time. Commented Apr 12, 2022 at 5:01
  • 2
    Your code seems to be correct. Just console output outside the for loop Commented Apr 12, 2022 at 5:02
  • this can help Commented Apr 12, 2022 at 5:03

4 Answers 4

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

answered Apr 12, 2022 at 5:18
Sign up to request clarification or add additional context in comments.

Comments

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
answered Apr 12, 2022 at 5:03

Comments

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.

answered Apr 12, 2022 at 5:11

Comments

0

Why not use URLSearchParams? and you can replace the URL with window.location.href

let url = new URL(`http://localhost?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`)
let params = new URLSearchParams(url.search).getAll("make")
console.log(params)
answered Apr 12, 2022 at 5:33

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.