Home > Software engineering >  Javascript - Adding new element to array using for loop
Javascript - Adding new element to array using for loop

Time:04-12

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 Premium&trim=2.0T S line Premium&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' ]?

CodePudding user response:

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 Premium&trim=2.0T S line Premium&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

CodePudding user response:

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 Premium&trim=2.0T S line Premium&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.

CodePudding user response:

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 Premium&trim=2.0T S line Premium&normalBodyStyle=Hatchback&normalBodyStyle=Sedan&odometer=13000-38000&internetPrice=20000-50000';

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

console.log(makes);

CodePudding user response:

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 Premium&trim=2.0T S line Premium&normalBodyStyle=Hatchback&normalBodyStyle=Sedan&odometer=13000-38000&internetPrice=20000-50000`)
let params = new URLSearchParams(url.search).getAll("make")
console.log(params)
  • Related