0

How to convert url specific parameters to array? I have used many types or parameter. I have tried below code with var types = ['A', 'C']; and its working.

But it's not working with url. I need to get community parameter from URL. How to convert url parameters to array? like this var types = ['business_center','wireless_access'];

http://demo/apartments/?price-min=200&price-max=2356&bedroom=3&bathroom=2&ap_features%5B%5D=washer+%26+dryer+connections&ap_features%5B%5D=dishwasher&community%5B%5D=business_center&community%5B%5D=clubhouse&community%5B%5D=covered_parking&submit=SEARCH

//var types = ['A', 'C'];
var i = document.location.href.lastIndexOf('?');
document.location.href.substr(i+1).replace(/community=/g,'').split('&');
$('input[name="community[]"]').prop('checked', function() {
  return $.inArray(this.value, types) !== -1;
});
2
  • what is your expected output Commented Feb 20, 2018 at 12:47
  • @xianshenglu var types = ['business_center','wireless_access']; Commented Feb 20, 2018 at 12:52

2 Answers 2

1

You can split the URL and make a JSON object out of it. Parameters will become keys and their values will be stored in a corresponding array.

var url = 'http://demo/apartments/?price-min=200&price-max=2356&bedroom=3&bathroom=2&ap_features%5B%5D=washer+%26+dryer+connections&ap_features%5B%5D=dishwasher&community%5B%5D=business_center&community%5B%5D=clubhouse&community%5B%5D=covered_parking&submit=SEARCH';

var ob = URLToArray(url);
console.log(ob); //output the whole key-value pair object
console.log(ob["community"]); //obtain the "community" values (Array)

function URLToArray(url) {
  var request = {};
  var pairs = url.substring(url.indexOf('?') + 1).split('&');
  for (var i = 0; i < pairs.length; i++) {
    if (!pairs[i]) //if empty parameter then continue to next
      continue;
    var pair = pairs[i].split('='); //split to separate values
    if (pair[0].indexOf('%') > -1) //community has some string attached to it starting with %, this cleans it
      pair[0] = pair[0].substring(0, (pair[0].indexOf('%')));
    var key = pair[0];
    if (key in request) //check if any value with same key is already stored
      request[key].push(pair[1]); //append to the array of already existing values
    else
      request[key] = [pair[1]]; //else make a new key-value pair, value is an array of elements
  }
  return request;
}

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

Comments

0

You can get the query string with document.location.search. Then use this snippet to get all the query string values. So with this result array you can filter or similar what you want. Is this what you looking for?

var url = '?price-min=200&price-max=2356&bedroom=3&bathroom=2&ap_features%5B%5D=washer+%26+dryer+connections&ap_features%5B%5D=dishwasher&community%5B%5D=business_center&community%5B%5D=clubhouse&community%5B%5D=covered_parking&submit=SEARCH';
var final = url.replace('?','').split('&').map(x=> x.split('=')[1]);

console.log(final);

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.