0
//example of an Object
var person = {
    firstName:"John",
    lastName:"Doe",
    age:50
};

Expected output : URL : www.domain.com/person?firstName=John&lastName=Doe&age=50

what is the best way to add it in query param ?

1
  • How about this for the last part , Object.entries(person).map(k=>k.join("=")).join('&') Commented Jun 24, 2020 at 15:43

2 Answers 2

1

We can take entries of the object then we can map and join it accordingly. Give this a try:

var person = { firstName:"John", lastName:"Doe", age:50 };

var result = `www.domain.com/person?${Object.entries(person).map(k=>k.join("=")).join('&')}`;

console.log(result);

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

Comments

0

I found this method :

 getQueryParamURL(url, param) {
        let query = '?'
        Object.keys(param).forEach((key, i, arr) => {
            query += key + '=' + param[key]
            if (arr[i + 1]) {
                query += '&';
            }
        })
        return url + query
    }

Use it :

let personObj = {
    firstName:"John",
    lastName:"Doe",
    age:50
};
let queryUrl = this.getQueryParamURL('www.domain.com/person',personObj )

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.