An object (POJO) holds query arguments and has two-binding to a form using some MVC framework. later the object needs to be converted to a query string that will be appended to an HTTP request.
function objectToQueryString(obj) {
return Object.keys(obj)
.filter(key => obj[key] !== '' && obj[key] !== null)
.map((key, index) => {
var startWith = index === 0 ? '?' : '&';
return startWith + key + '=' + obj[key]
}).join('');
}
Any pitfalls? IE9+ is what we support.
Example input:
{isExpensive:true, maxDistance:1000,ownerName:'Cindy',comment:''}
And its expected output:
?isExpensive=true&maxDistance=1000&ownerName=cindy
obj
for reference? – Quill Apr 20 at 22:34