title: JS/jQ make a URL builder function without duplicate
body: <p>First I should say, I just want to solve this as logic, not my real code is jQuery or JavaScript, because I learning <code>reactjs</code> I trying to solve this logic with jQ or JS then do it in React. So I don't want a solution like <code>$.param</code> and also don't want to replace last <code>&amp;</code> with something, I looking for to make a URL Builder function, What I trying to do is add URL parameter (Query String) What I tried so far is:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>let obj = {}
  let array = []
  let url = 'http://www.example.com/foo';
  
  function buildUrl(base, key, value) {
    var sep = (base.indexOf('?') &gt; -1) ? '&amp;' : '?';
    return base + sep + key + '=' + value;
  }
  
  $('#btn1').click(function() {
    obj = {
      name: 'province',
      value: 11
    }
    array.push(obj)
    console.log('param 1 added!')
  });
  
  $('#btn2').click(function() {
    obj = {
      name: 'city',
      value: 2
    }
    array.push(obj)
    console.log('param 2 added!')
  });
  
  $('#build').click(function() {
    $.each(array, function(i, v) {
      url = buildUrl(url, v.name, v.value);
    });
    console.log(url)
  });</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;
  
  &lt;button id="btn1"&gt;1. Click this&lt;/button&gt;
  &lt;kbd&gt;-&gt;&lt;/kbd&gt;
  &lt;button id="btn2"&gt;2. Then click this&lt;/button&gt;
  &lt;kbd&gt;=&lt;/kbd&gt;
  &lt;button id="build"&gt;Make URL&lt;/button&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>Somehow, it add province twice, I don't how to make this function to avoid duplicate parameters, any idea?</p>
  
  <p><em>side note: I have many btn to build parameter, so I can't detect value each time!</em></p>