variables - jQuery - How to remove duplicate parameter on string? (not array) -


i have 2 variable value , character '&' determinate parameter (like url parameter).

note: not parameter url , not array!

variable1 = value1=bla&value2=ble&page=test&value4=blo&test=blu&car=red variable2 = page=index&car=blue&other=yellow 

i need compare 2 variables , eliminate duplicate parameters, , if have same parameter on variable1 , variable2, need exclude parameter variable1 , use parameter variable2.

like this:

value1=bla&value2=ble&page=index&value4=blo&test=blu&car=blue&other=yellow 

how this?

i've build jsfiddle out. i've attempted keep vanilla possible , left comments.

quickly – this parses parameter string , replaces duplicate parameter names last value found. couldn't tell needed question; maybe going.

here's jsfiddle code reference:

/**  * open console see what's going on  */  function parse(str) {     // parse split string along &     // , loop on result      var keyvalues, i, len, el,          parts, key, value, result;      result    = {};     septoken  = '&';     keyvalues = str.split('&');        = 0;     len = keyvalues.length;      for(i; i<len; i++) {         el    = keyvalues[i];         parts = el.split('=');         key   = parts[0];         value = parts[1];          // replace duplicate param          // last value found         result[key] = value;     }      return result; }  function serialize(data) {     // serialize loops on data , constructs new string      var prop, result, value;      result = [];      for(prop in data) {         if (data.hasownproperty(prop)) {             value = data[prop];              // push each seriialized key value array             result.push(prop + '=' + value);         }     }      // return resulting array joined on &     return result.join("&"); }  function run() {     // run example      var paramstr, data, result;       // paramstr has duplicate param, value1     paramstr = 'value1=bla&value1=foop&value2=ble&page=test&value4=blo&test=blu&car=red';     data     = parse(paramstr);     result   = serialize(data);      return result;  }  console.log(run()); 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -