0

I have the following URL:

http://mydomain/Forwards?searchValue[]=Nike+Webstore&searchValue[]=Bodyman&category_filter[]=Animals+%26+Pet+Supplies&category_filter[]=Fashion&country_filter[]=Aland+Islands&country_filter[]=American+Samoa

This url contains alot of paramters that are sent as an array:

Now i wish to get each individual array and its value out

in the above example the result should be something like this:

searchValue = array(
[0] = 'Nike Webstore'
[1] = 'Bodyman'
);

category_filter = array(
[0] = 'Animals & Pet Supplies'
[1] = 'Fashion'
);

country_filter = array(
[0] = 'Aland Islands'
[1] = 'American Samoa'
);

is it possible to get it out like this and if so how? i have attempted with the following:

 decodeURIComponent(
    (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]

However this only returned 1 value (Nike Webstore) in my example.

4
  • i just made a quick code that does what you are trying to do.... but i havent tested it.. try changing the variable " url " and see if it works... here is the link... jsbin.com/UYILAzU/2/edit?js,console Commented Oct 15, 2013 at 10:03
  • @PsychHalf il test it hang on :D Commented Oct 15, 2013 at 10:13
  • okay.. so how did the test go?? Commented Oct 15, 2013 at 10:32
  • @PsychHalf just finished it when perfect can you post it as an answer? Commented Oct 15, 2013 at 10:54

2 Answers 2

2

as parameters are an array. the below code will work just fine..

// our test url
var url ="http://mydomain/Forwards?searchValue[]=Nike+Webstore&searchValue[]=Bodyman&category_filter[]=Animals+%26+Pet+Supplies&category_filter[]=Fashion&country_filter[]=Aland+Islands&country_filter[]=American+Samoa" ;
// filtering the string..
var paramsList = url.slice(url.indexOf("?")+1,url.length) ;
var filteredList =  paramsList.split("&") ;

// an object to store arrays
var objArr = {} ;

// the below loop is obvious... we just remove the [] and +.. and split into pair of key and value.. and store as an array...
for (var i=0, l=filteredList.length; i <l; i +=1 ) {
  var param = decodeURIComponent(filteredList[i].replace("[]","")).replace(/\+/g," ") ;
  var pair = param.split("=") ;
  if(!objArr[pair[0]]) {  objArr[pair[0]] = [] ;}
  objArr[pair[0]].push(pair[1]);
}

console.log(objArr);

which will give us....

[object Object] {
  category_filter: ["Animals & Pet Supplies", "Fashion"],
  country_filter: ["Aland Islands", "American Samoa"],
  searchValue: ["Nike Webstore", "Bodyman"]
}

hope this helps.. :D

Sign up to request clarification or add additional context in comments.

Comments

1

Try to see if this pattern works for you

(?:\?|&)(.+?)=([A-Z+%0-9]+)

Example here

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.