node.js with express how to remove the query string from the url

后端 未结 9 1414
孤街浪徒
孤街浪徒 2020-12-29 19:41

I have a button that is performing a get to my page and adding a filter to the query string. My code applies that filter to the grid...but the user can remove/edit that fil

9条回答
  •  青春惊慌失措
    2020-12-29 20:23

    use this method to remove specific query parameter from URL.

    /**
         * remove query parameters from actual url
         * @param {*} params paramerters to be remove, e.g ['foo', 'bar'] 
         * @param {*} url actual url 
         */
        function removeQueryParam(parameters = [], url) {
            try {
                var urlParts = url.split('?');
                var params = new URLSearchParams(urlParts[1]);
                parameters.forEach(param => {
                    params.delete(param);
                })
                return urlParts[0] + '?' + params.toString();
            } catch (err) {
                console.log(err);
                return url;
            }
        }
    
        console.log(removeQueryParam(["foo"], "/foo?foo=foo&bar=bar"));

    Above example will return /foo?bar=bar

提交回复
热议问题