-1

i have a url like this

http://localhost:9001/search_results?r

I'm using querystring to take the value of 'r' like this. (I'm using a react library)

componentDidMount() {

        const parsed = queryString.parse(location.search);
        console.log("Character",parsed);

}

In my log i get a value like {r: null} How can i get only the value of 'r' from this?

CraZyDroiD
  • 6,622
  • 30
  • 95
  • 182
  • 4
    there is no value assigned for r, set url like r=something to get the value of r – sanatsathyan Jan 29 '18 at 05:48
  • 1
    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) – RIYAJ KHAN Jan 29 '18 at 05:49
  • Possible duplicate of [Getting query parameters from react-router hash fragment](https://stackoverflow.com/questions/29852998/getting-query-parameters-from-react-router-hash-fragment) – Mayank Shukla Jan 29 '18 at 05:51
  • @Rajesh He's trying to find `r` from the url. – Blue Jan 29 '18 at 06:16

1 Answers1

3

Query string parameters may have both keys and values, such as http://localhost:9001/search_results?key1=value1&key2=value2. For this reason, you're being given an object with both keys and their values. ?r is a query string with a key "r" with no value (null), thus {r: null}.

If you want a simple array of the keys, and don't care for the values you can simply use Object.keys() then select the first one:

var parsed = {r: null};

var keys = Object.keys(parsed);

console.log(keys);
console.log(keys[0]);
lotyrin
  • 192
  • 8
Blue
  • 22,608
  • 7
  • 62
  • 92