-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?

4

1 Answer 1

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]);

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

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.