Thanks to visit codestin.com
Credit goes to github.com

Skip to content

lcostash/create-object-from-get-params

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

Create Object from GET parameters

With this simple function we can map GET parameters from the url into the Object.

function createObjectFromGetParams(text) {
    const result = {};
    if (result.length === 0) return result;

    const params = text.split('&');
    params.forEach(param => {
        const [path, value] = param.split('=');
        const keys = path.split('.');
        let current = result;

        keys.forEach((key, index) => {
            if (index === keys.length - 1) {
                current[key] = decodeURIComponent(value);
            } else {
                if (!current[key]) current[key] = {};
                current = current[key];
            }
        });
    });

    return result;
}

For example, we have the next params:

const url = 'user.name.firstname=John&user.name.lastname=Doe&user.role=Administrator&setup.theme=Space%20Gray&setup.language=English&filter=';
const obj = createObjectFromGetParams(url);

The results of console.log(obj); will be the next object:

{
  user: {
    name: { 
      firstname: 'John', 
      lastname: 'Doe'
    },
    role: 'Administrator'
  },
  setup: { 
    theme: 'Space Gray', 
    language: 'English'
  },
  filter: ''
}

The results of console.log(obj.user.name.firstname); will display:

John

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published