uri.js add array key to querystring code example

Example 1: js object to querystring

const params = {foo:"Hi", bar:"there"}
const queryString = new URLSearchParams(params).toString();

console.log(queryString);

Example 2: javascript object to query string

function objectToQueryString(obj) {
  var str = [];
  for (var p in obj)
    if (obj.hasOwnProperty(p)) {
      str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
    }
  return str.join("&");
}

var person = { first_name : "Marty",last_name : "Mcfly"};
var queryString=objectToQueryString(person); //"first_name=Marty&last_name=Mcfly"