Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This function takes in an array of objects:
- [
- {
- param: 'value'
- },
- {
- anotherParam: 'another value'
- }
- ]
- */
- function constructQueryString(params) {
- if (typeof params !== 'object' || ! params.length) {
- throw new Error('Params must be an array');
- }
- let string = '';
- // Loop through params
- for (let i = 0; i < params.length; i++) {
- const key = Object.keys(params[i])[0];
- const value = params[i][key];
- if (i === 0) {
- string += `?${ key }=${ value }`;
- } else {
- string += `&${ key }=${ value }`;
- }
- }
- return encodeURI(string);
- }
Add Comment
Please, Sign In to add comment