JS - Add property to JSON if query string exist in URL

0 votes
172 views
added Jul 6, 2018 in Javascript by LC Marshal Captain (25,790 points)
edited Jul 12, 2018 by LC Marshal
//example URL: http://localhost:8080/?uid=1&nid=18&act=fund#/
//example URL: http://localhost:8080/?uid=1&nid=12&act=buy#/
(async () => {
  function getUrlParam() {
      let params = {};
      let parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
          params[key] = value;
      });
      return params;
  }

  let status = null,
      actParam = getUrlParam()['act'];   

  if(actParam == 'buy#/') {
    status = 1;
  } else if(actParam == 'fund#/') {
    status = 2;
  }

  let apiUrl = `${mainSiteUrl}/api/property-units.json?property_nid=${this.pid}`;
  if(status) apiUrl += `&status=${status}`;

  const { data } = await this.axios.get(apiUrl);
  this.setListOfUnits(data);
})
lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...