Convert URL parameters to a JavaScript Object Last Updated : 26 Jul, 2023 Comments Improve Suggest changes Like Article Like Report Given an URL with parameters, The task is to get those parameters and convert them to a JavaScript Object using javascript. we're going to discuss a few techniques. Below is the following method to convert URL parameters to a JavaScript Object: Using replace() MethodUsing split() MethodUsing for ...of loopMethod 1: Using replace() MethodThis method searches a string for a defined value, or a regular expression, and returns a new string with the replaced defined value. Syntax: string.replace(searchVal, newvalue)Example: This example first takes the URL's parameter portion and then uses the replace() method to form an object. JavaScript let search = 'https://www.geeksforgeeks.org/?param_1=val_1&x=7&y=9'; console.log(search); search = search.split('?')[1]; function GFG_Fun() { console.log('{"' + decodeURI(search) .replace(/"/g, '\\"').replace(/&/g, '","') .replace(/=/g, '":"') + '"}'); } GFG_Fun() Outputhttps://www.geeksforgeeks.org/?param_1=val_1&x=7&y=9 {"param_1":"val_1","x":"7","y":"9"}Method 2: Using split() MethodThis method is used to split a string into an array of substrings and returns the new array. Syntax: string.split(separator, limit)Example: This example implements the same functionality but with a different approach. It first takes the parameter portion of the URL and then uses the replace() method to form an object. JavaScript let search = 'https://www.geeksforgeeks.org/?param_1=val_1&x=7&y=9'; console.log(search); search = search.split('?')[1]; function GFG_Fun() { console.log('{"' + search.replace(/&/g, '", "') .replace(/=/g, '":"') + '"}', function (key, value) { return key === "" ? value : decodeURIComponent(value) }); } GFG_Fun() Outputhttps://www.geeksforgeeks.org/?param_1=val_1&x=7&y=9 {"param_1":"val_1", "x":"7", "y":"9"} [Function (anonymous)]Method 3: Using for ...of loopIn this we use Url.search for getting the parametersNow that passes as a parameter to the functionNow use the for...of loop for accessing all keys and valuesExample: JavaScript function paramsToObject(params) { const obj = {}; const para = new URLSearchParams(params); for (const [key, value] of para) { if (obj.hasOwnProperty(key)) { if (Array.isArray(obj[key])) { obj[key].push(value); } else { obj[key] = [obj[key], value]; } } else { obj[key] = value; } } return obj; } let search = 'https://www.geeksforgeeks.org/?param_1=val_1&x=7&y=9'; const url = new URL(search); const params = paramsToObject(url.search); console.log(params); Output{ param_1: 'val_1', x: '7', y: '9' } Comment More infoAdvertise with us Next Article Convert URL parameters to a JavaScript Object P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies javascript-object JavaScript-Questions Similar Reads How to Pass Object as Parameter in JavaScript ? We'll see how to Pass an object as a Parameter in JavaScript. We can pass objects as parameters to functions just like we do with any other data type. passing objects as parameters to functions allows us to manipulate their properties within the function. These are the following approaches: Table of 3 min read How to get an object containing parameters of current URL in JavaScript ? The purpose of this article is to get an object which contains the parameter of the current URL. Example: Input: www.geeksforgeeks.org/search?name=john&age=27 Output: { name: "john", age: 27 } Input: geeksforgeeks.org Output: {} To achieve this, we follow the following steps. Create an empty obj 2 min read How to convert an object to string using JavaScript ? To convert an object to string using JavaScript we can use the available methods like string constructor, concatenation operator etc. Let's first create a JavaScript object. JavaScript // Input object let obj_to_str = { name: "GeeksForGeeks", city: "Noida", contact: 2488 }; Examp 4 min read Converting JSON text to JavaScript Object Pre-requisite: JavaScript JSON JSON (JavaScript Object Notation) is a lightweight data-interchange format. As its name suggests, JSON is derived from the JavaScript programming language, but itâs available for use by many languages including Python, Ruby, PHP, and Java and hence, it can be said as l 3 min read How to Convert Object to Array in JavaScript? In this article, we will learn how to convert an Object to an Array in JavaScript. Given an object, the task is to convert an object to an Array in JavaScript. Objects and Arrays are two fundamental data structures. Sometimes, it's necessary to convert an object to an array for various reasons, such 4 min read JavaScript - Convert an Array to an Object These are the following ways to convert an array to an Object:1. Using JavaScript Object.assign() method The first approach is using the Object.assign() method. This method copies the values of all enumerable properties from source objects(one or more) to a target object.JavaScriptlet a = [1, 2, 3, 2 min read How to get URL Parameters using JavaScript ? To get URL parameters using JavaScript means extracting the query string values from a URL. URL parameters, found after the ? in a URL, pass data like search terms or user information. JavaScript can parse these parameters, allowing you to programmatically access or manipulate their values.For getti 3 min read JavaScript- Convert an Object to JS Array Objects in JavaScript are the most important data type and form the building blocks for modern JavaScript. These objects are quite different from JavaScriptâs primitive data types (Number, String, Boolean, null, undefined, and symbol). Methods to convert the Objects to JavaScript Array:1. Using Obje 3 min read How to Convert String to Array of Objects JavaScript ? Given a string, the task is to convert the given string to an array of objects using JavaScript. It is a common task, especially when working with JSON data received from a server or API. Below are the methods that allow us to convert string to an array of objects:Table of ContentUsing JSON.parse() 4 min read How to add a parameter to the URL in JavaScript ? Given a URL the task is to add parameter (name & value) to the URL using JavaScript. URL.searchParams: This read-only property of the URL interface returns a URLSearchParams object providing access to the GET-decoded query arguments in the URL. Table of Content Using the append methodUsing set m 2 min read Like