Matias Cicero
Matias Cicero

Reputation: 26281

Mapping object to key=value string in one line

Is there a way to convert this object:

{
    lang: 'en-us',
    episode: 12
}

To a string with the following format?

"lang=en-us&episode=12"

Much like mapping an object to a query string, where each property is a query parameter.

I can do it like this:

var parameters = [];
for(var prop in obj)
   parameters.push(prop + '=' + obj[prop]);
return parameters.join('&');

But I was looking for a one-line solution. Is this possible?

PS: I cannot use jQuery and any of it utility functions. The solution must be in pure JavaScript.

Upvotes: 6

Views: 17596

Answers (5)

Aravin
Aravin

Reputation: 7067

If the value contains equalTo (=) symbol, the accepted solution will not work.

Example

key1=a,key2=b,keyc=keyof=thec
accesskey=absdsfdfsa===

Solution:

Better to use Regex,

const a =
  'AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;DefaultEndpointsProtocol=http;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;QueueEndpoint=http://127.0.0.1:10001/devstoreaccount1;TableEndpoint=http://127.0.0.1:10002/devstoreaccount1';

  const output = {};

  a.split(';').forEach((v, i) => {
    const [key, value] = v.split(RegExp(/=(.*)/));
    output[key] = value;
  })

  console.log(output);

output Note: AccountKey contains == in value

{
  AccountName: 'devstoreaccount1',
  AccountKey: 'Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==',
  DefaultEndpointsProtocol: 'http',
  BlobEndpoint: 'http://127.0.0.1:10000/devstoreaccount1',
  QueueEndpoint: 'http://127.0.0.1:10001/devstoreaccount1',
  TableEndpoint: 'http://127.0.0.1:10002/devstoreaccount1'
}

Upvotes: 0

tdy
tdy

Reputation: 41327

  • Object.entries (ES6)

    Object.entries returns [key, value] pairs, so we can destructure and map the pairs directly into ${key}=${value}:

    Object.entries(params).map(([key, value]) => `${key}=${value}`).join('&');
    // "lang=en-us&episode=12"
    

  • URLSearchParams

    For the specific use case of query strings, use URLSearchParams.toString:

    let searchParams = new URLSearchParams(params);
    
    searchParams.toString();
    // "lang=en-us&episode=12"
    

    URLSearchParams provides an interface with common utility methods, e.g.:

    searchParams.get('episode');
    // "12"
    
    searchParams.sort();
    // "episode=12&lang=en-us"
    
    searchParams.set('episode', 123456);
    // "episode=123456&lang=en-us"
    

Upvotes: 3

Craig Wayne
Craig Wayne

Reputation: 5060

All the other answers work perfectly,

However I've found that you can use URLSearchParams for the same thing...

var myObj = {keyOne: 'keyOneValue', keyTwo: 'keyTwoValue'};
var queryParams = new URLSearchParams(myObj).toString();
console.log(queryParams);
// keyOne=keyOneValue&keyTwo=keyTwoValue

see: https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams

Upvotes: 2

Deftwun
Deftwun

Reputation: 1232

You could use

var myObj ={"lang": "en-us", "episode": 12};
var str = Object.keys(myObj).map(key => key+"="+myObj[key]).join("&");

Whether or not this is any more readable is another question :)

Upvotes: 4

ssube
ssube

Reputation: 48267

You can use Array.prototype.map on the Object.keys array:

var data = {"lang": "en-us", "episode": 12};
var str = Object.keys(data).map(function (key) { 
  return "" + key + "=" + data[key]; // line break for wrapping only
}).join("&");
console.log(str);

With ES6, this becomes even more terse:

var data = {"lang": "en-us", "episode": 12};
var str = Object.keys(data).map(key => `${key}=${data[key]}`).join("&");
console.log(str);

Upvotes: 17

Related Questions