Open In App

Lodash _.fromQuery() Method

Last Updated : 04 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Lodash _.fromQuery() method is used to convert the given URL Query String into an equivalent JavaScript object.

Syntax:

_.fromQuery( URL_Query);

Parameters:

This method accepts a single parameter as mentioned above and described below:

  • URL_Query: This method takes a URL Query String to convert into a JavaScript object.

Return Value:

This method returns the equivalent JavaScript Object.

Note: This will not work in normal JavaScript because it requires the lodash.js contrib library to be installed. Lodash.js contrib library can be installed using npm install lodash-contrib –save.

Example 1: In this example, the lodash-contrib library is used to convert a URL query string into a JavaScript object, extracting parameters like “name” with the value “ferret” and “color” with the value “purple” from the provided URL.

Javascript




// Defining lodash contrib variable
let _ = require('lodash-contrib');
 
let s  = _.fromQuery(
   
console.log("The generated JavaScript Object is : ", s);


Output:

The generated JavaScript Object is :
Object {
color: "purple" ,
https://geeksforgeeks.org/path/to/page?name: "ferret"
}

Example 2: In this example, the lodash-contrib library is employed to convert a URL query string into a JavaScript object, parsing parameters from the given URL, such as “ref” with the value “gfg_header.”

Javascript




// Defining lodash contrib variable
let _ = require('lodash-contrib');
 
let s  = _.fromQuery(
   
console.log("The generated JavaScript Object is : ", s);


Output:

The generated JavaScript Object is :
Object {https://practice.geeksforgeeks.org/courses/?ref: "gfg_header"}

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads