1

In Ruby on Rails you can use to_query to convert a hash into a string suitable for use as a URL query string.

For example:

{ :name => 'David', :nationality => 'Danish' }.to_query # => "name=David&nationality=Danish"

Is there an equivalently easy way to do this in jQuery or javascript?

lucas
  • 1,050
  • 12
  • 21
  • This has been asked before https://stackoverflow.com/questions/3308846/serialize-object-to-query-string-in-javascript-jquery For some possible solutions. – dops Jan 27 '18 at 16:56
  • The [the first hit](https://www.google.com/search?client=ubuntu&channel=fs&q=jquery+to_query&ie=utf-8&oe=utf-8) according to the googlies. – jvillian Jan 27 '18 at 17:05

1 Answers1

2

You can use $.param() function.

var query = $.param({ name: 'David', nationality: 'Danish' });
console.log(query);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
halfer
  • 19,824
  • 17
  • 99
  • 186
Ele
  • 33,468
  • 7
  • 37
  • 75