0

I am using query-string(version 5) and wondering is there away to remove a query string with it?

results?condition=Used

I want to remove condition completely from the url.

If not any alternative way?

chobo2
  • 83,322
  • 195
  • 530
  • 832
  • Do you just want to keep `results`? If so the easiest way would be to use `.indexof("?")` and `substr()` to just extract the part before the query parameters. No additional library needed. – Auskennfuchs Sep 18 '18 at 22:21
  • Well I will be adding and deleting parameters on demand. I could have something like results?condition=Used&year=2010 then I might want to add parameter results?condition=Used,New&year=2010 or it could be r results?year=2010. So what I like about this library it makes everything into an object and it also encodes the url. – chobo2 Sep 18 '18 at 22:24
  • The library you're using parses a Query string into an object. You can just add or remove properties from that object and use the stringify function from the library to build a new Query string. – icecub Sep 18 '18 at 22:27
  • Ok, I guess I can delete it myself just wanted to make sure if I was missing some builtin function. – chobo2 Sep 18 '18 at 22:28
  • Ok so you can use destructuring to remove a property from the parsed object from query-string. `const parsedQueryString = queryString.parse(...);{condition,...rest} = parsedQueryString;` – Auskennfuchs Sep 18 '18 at 22:28
  • Possible duplicate of [How can I get query string values in JavaScript?](https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – MaxG Sep 18 '18 at 22:32

1 Answers1

0

To process url, I prefer to use url package. For you case, I will do

const url = require('url');

const targetUrl = 'results?condition=Used&year=2010';
const parsedUrl = url.parse(targetUrl, true);

delete parsedUrl.search; // need to delete it so we can construct new query
delete parsedUrl.query.condition; // delete "condition"

console.log(url.format(parsedUrl)); // results?year=2010
deerawan
  • 8,002
  • 5
  • 42
  • 51