4

My question is simple. I know query-string npm do the job, but have the special case. My query params look like this:

params={

foo:'bar',
data:[1,2,3],
data2:[4,5,6]
} 

i need output something like ?foo=bar&data=1,2,3&data2=4,5,6

not what done by query-sting. query-sting output it as

?foo=bar&data=1 &data=2&data=3..........

2
  • First have you tried anything? Second, do you need those spaces before and after &? Commented Aug 31, 2017 at 12:32
  • @Rajesh i try using query-string, with arrayIndex as optional parameter. Commented Aug 31, 2017 at 12:35

4 Answers 4

2

The Problem you are facing can be solved as follow:

const queryString = require('query-string')

const seacrh=queryString.stringify(params)

the above line produce search=?foo=bar&data=1%0C2%0C3&data2=4%0C5%0C6

In order to Remove illogical character, just decode the url using below lines of code

const search1=decodeURIComponent(seacrh);

The decodeURIComponent decode those illogical string.

Sign up to request clarification or add additional context in comments.

2 Comments

What does a seacrh have to do with this? ^^
@BotNet, ya i know thats unnecessary. I can even write it as :::::::::::: const result =decodeURIComponent(queryString.stringify(params))
0

The following should do what your after.

I've just used a array.join to get your data= into the correct format..

var params={
  foo:'bar',
  data:[1,2,3],
  data2:[4,5,6]
};

//?foo=bar&data=1,2,3 & data2=4,5,6

console.log(
  `?foo=${params.foo}&data=${params.data.join(',')}&data2=${params.data2.join(',')}`);

1 Comment

i try this with sth like newdata=params.data.join(','). Then pass newdata as params. eg params={foo:'bar',data:newdata}. But the output is not as expected as it show sth like this. ?foo='bar'&data=1%0C2
0

Here's a function that does the tick:

var params = {
  foo:   'bar',
  data:  [1,2,3],
  data2: [4,5,6]
};

function toQuery (params) {
  var queryString = '';
  // Check that we've been suplied with an object.
  if (params.constructor !== Object) {
    return queryString;
  }
  // Iterate over parameters
  for (var param in params) {
    if (!params.hasOwnProperty(param)) {
      continue;
    }
    if (typeof params[param] === 'string' || typeof params[param] === 'number') {
      queryString += '&' + param + '=' + params[param];
    }
    if (params[param].constructor === Array && params[param].length) {
      queryString += '&' + param + '=' + params[param].join(',');
    }
  }
  // Change first '&' to '?'
  if (queryString.length) {
    queryString = '?' + queryString.substring(1);
  }
  // Return the query string, empty string if no usable parameters.
  return queryString;
}

console.log(toQuery(params));

Comments

0

If you need to build your search:

let params = {
  foo: 'bar',
  data: [1, 2, 3],
  data2: [4, 5, 6]
};

let queryString = Object.keys(params).map(
  key => `${key}=${params[key]}`
).join('&');

console.log(queryString)

  • the string constructor will automatically comma-delimit the array
  • this is very elementary and does not include any URI encoding, etc

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.