daily pastebin goal
43%
SHARE
TWEET

Untitled

a guest Aug 10th, 2018 56 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. const params = {
  2.     "name": ["alpha &", "beta"],
  3.     "id": ["one", "&two=2"],
  4.     "from": 1533735971000,
  5.     "to": 1533822371147,
  6.     "status": true
  7. };
  8.    
  9. const output = Object.entries(params).map(
  10.   (pair) => {
  11.     let [key, val] = pair;
  12.     // normalize val to always an array
  13.     val = (val instanceof Array) ? val : [val];
  14.     // encode key only once
  15.     key = encodeURIComponent(key);
  16.     // then make a different query string for each val member
  17.     return val.map(
  18.       (member) => `${key}=${encodeURIComponent(member)}`
  19.     ).join('&');
  20.   }
  21. ).join('&');
  22.    
  23. return val.map(
  24.       (member) => `${key}=${encodeURIComponent(member)}`
  25.     ).join('&');
  26.    
  27. return val.map(
  28.   (member) => (member ? `${key}=${encodeURIComponent(member)}` : `${key}`)
  29. ).join('&');
  30.    
  31. return val.reduce(
  32.   (result, member) => {
  33.     if (member) {
  34.       result.push(`${key}=${encodeURIComponent(member)}`);
  35.     }
  36.     return result;
  37.   },
  38.   []
  39. ).join('&');
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top