Skip to main content
added 3781 characters in body
Source Link

You can use those tow functions, but make sur you don't use '-' on your object keys.

// convert obj to url params function objToUrlParams(obj) { let toUrlParams = (obj, prefex = '') => { // create url params let urlParams = ""; // loop through obj for (let key in obj) { let val = obj[key];  if (val == null) continue; if (val == undefined) continue; // if(val == '') continue;  // if val is an object then call toUrlParams if (val instanceof Array) { // convert val.forEach((item, index)from =>Array to object  let valToObj = {}; val.forEach((v, i) => {  urlParams += prefex + key + '=' + item + '&'; valToObj[i] = v; });  urlParams = urlParams.slice(0, -1); } else if (val instanceof Object) {   urlParams += toUrlParams(val, key += '-');valToObj; }    elselet newPrefex = prefex + key;  if (val instanceof Object) { urlParams += prefextoUrlParams(val, newPrefex + key'-'); } else { urlParams += newPrefex + '=' + val; } urlParams += '&'; } // remove last & urlParams = urlParams.slice(0, -1); // return url params return urlParams; } // encodeURI return encodeURI(toUrlParams(obj)); } // convert url params to obj function urlParamsToObj(urlParams) {  // decodeURI urlParams = decodeURI(urlParams);  let toObj = (urlParams) => { let obj = {}; let urlParamsArr = urlParams.split('&'); let subUrlParramsObj = {}; // loop through urlParams for (let i = 0; i < urlParamsArr.forEach((item)length; =>i++) { let item = urlParamsArr[i]; // get key and value let key = item.split('=')[0]; let val = item.split('=')[1];[1] ?? null; let keys = key.split('-');  if (val == "null") { val = null; } else if (val == "undefined") { val = undefined; } else if (val == "true") { val = true; } else if (val == "false") { val = false; } else if (val == "NaN") { val = NaN; } else if (val == "Infinity") { val = Infinity; }  // if keys length is 1 then set obj[key] to val if (keys.length == 1) { // check if obj contains key if (obj.hasOwnProperty(key)) { // if obj[key] is an array then push val if (obj[key] instanceof Array) { obj[key].push(val); } else { // create array and push val obj[key] = [obj[key], val]; } }  else { obj[key] = val; } } // if keys length is 2 then set obj[keys[0]][keys[1]] to val else if (keys.length > 1) { let key0 = keys[0]; // check if subUrlParramsObj contains keys[0] if (!subUrlParramsObj[key0]) { subUrlParramsObj[key0] = []; } // remove keys[0] from keys keys.shift(); // join keys with - key = keys.join('-'); let param = key + '=' + val; // add param to subUrlParramsObj[keys[0]] subUrlParramsObj[key0].push(param); } }); // loop through subUrlParramsObj for (let key in subUrlParramsObj) { // join subUrlParramsObj[key] with & let val = subUrlParramsObj[key].join('&'); // set obj[key] to val obj[key] = toObj(val); } // return obj return obj; } return checkIfObjShouldBeArrayAndConvert(toObj(urlParams)); } // check if object should be converted to array, if its keys are numbers function checkIfObjShouldBeArrayAndConvert(obj) { // if obj is an array if (obj instanceof Array) { // loop through obj obj.forEach((item, i) => { // check if item is an object if (item instanceof Object) { // convert item to array obj[i] = checkIfObjShouldBeArrayAndConvert(item); } }); // return obj return obj; } // check if all keys are numbers let canConvertToArray = true; for (let key in obj) { // get value let val = obj[key];  // check if value is an object or Array if (val instanceof Object || val instanceof Array) { obj[key] = checkIfObjShouldBeArrayAndConvert(val); } if (isNaN(key)) { canConvertToArray = false; } } // order obj by keys let orderedObj = {}; Object.keys(obj).sort().forEach(function(key) { orderedObj[key] = obj[key]; }); // check if the first key is 0 if (Object.keys(orderedObj)[0] != 0) { canConvertToArray = false; } // check if the keys step is 1 let keys = Object.keys(orderedObj); // loop through keys for (let i = 0; i < keys.length - 1; i++) { // get key let key = keys[i]; // get next key let nextKey = keys[i + 1]; // get key step let keyStep = nextKey - key; // check if key step is 1 if (keyStep != 1) { canConvertToArray = false; break; } } // if all keys are numbers then convert obj to array if (canConvertToArray) { let arr = []; for (let key in orderedObj) { arr.push(orderedObj[key]); } return arr; } // return obj return obj; } // add params to url function addParamsToUrl(params, url = window.location.href) { // check if url has params if (url.indexOf('?') == -1) { url += '?'; } else { url += '&'; } return url + params ?? ''; } function addObjToUrl(obj, url = window.location.href) { return addParamsToUrl(objToUrlParams(obj), url); } // extract params from url function extractParamsFromUrl(url = window.location.href) { return urlParamsToObj(url.split('?')[1]); } // test let urlParams = objToUrlParams({ a: 1, b: "zad", c: { d: 2, e: "f" }, j: [1, 2, 3, 4]  }); console.log(urlParams); // a=1&b=zad&c-d=2&c-e=f&j=1&j=2&j=3&j=4 let obj = urlParamsToObj(urlParams); console.log(obj); // { "a": "1", "b": "zad", "j": [ "1", "2", "3", "4" ], "c": { "d": "2", "e": "f" } }

You can use those tow functions, but make sur you don't use '-' on your object keys.

// convert obj to url params function objToUrlParams(obj) { let toUrlParams = (obj, prefex = '') => { // create url params let urlParams = ""; // loop through obj for (let key in obj) { let val = obj[key]; // if val is an object then call toUrlParams if (val instanceof Array) { val.forEach((item, index) => { urlParams += prefex + key + '=' + item + '&'; });  urlParams = urlParams.slice(0, -1); } else if (val instanceof Object) {   urlParams += toUrlParams(val, key + '-'); } else { urlParams += prefex + key + '=' + val; } urlParams += '&'; } // remove last & urlParams = urlParams.slice(0, -1); // return url params return urlParams; } // encodeURI return encodeURI(toUrlParams(obj)); } // convert url params to obj function urlParamsToObj(urlParams) { let toObj = (urlParams) => { let obj = {}; let urlParamsArr = urlParams.split('&'); let subUrlParramsObj = {}; // loop through urlParams urlParamsArr.forEach((item) => { // get key and value let key = item.split('=')[0]; let val = item.split('=')[1]; let keys = key.split('-'); // if keys length is 1 then set obj[key] to val if (keys.length == 1) { // check if obj contains key if (obj.hasOwnProperty(key)) { // if obj[key] is an array then push val if (obj[key] instanceof Array) { obj[key].push(val); } else { // create array and push val obj[key] = [obj[key], val]; } }  else { obj[key] = val; } } // if keys length is 2 then set obj[keys[0]][keys[1]] to val else if (keys.length > 1) { let key0 = keys[0]; // check if subUrlParramsObj contains keys[0] if (!subUrlParramsObj[key0]) { subUrlParramsObj[key0] = []; } // remove keys[0] from keys keys.shift(); // join keys with - key = keys.join('-'); let param = key + '=' + val; // add param to subUrlParramsObj[keys[0]] subUrlParramsObj[key0].push(param); } }); // loop through subUrlParramsObj for (let key in subUrlParramsObj) { // join subUrlParramsObj[key] with & let val = subUrlParramsObj[key].join('&'); // set obj[key] to val obj[key] = toObj(val); } // return obj return obj; } return toObj(urlParams); } // test let urlParams = objToUrlParams({a:1,b:"zad",c:{d:2,e:"f"},j:[1,2,3,4]}); console.log(urlParams); // a=1&b=zad&c-d=2&c-e=f&j=1&j=2&j=3&j=4 let obj = urlParamsToObj(urlParams); console.log(obj); // { "a": "1", "b": "zad", "j": [ "1", "2", "3", "4" ], "c": { "d": "2", "e": "f" } }

You can use those functions, but make sur you don't use '-' on your object keys.

// convert obj to url params function objToUrlParams(obj) { let toUrlParams = (obj, prefex = '') => { // create url params let urlParams = ""; // loop through obj for (let key in obj) { let val = obj[key];  if (val == null) continue; if (val == undefined) continue; // if(val == '') continue;  // if val is an object then call toUrlParams if (val instanceof Array) { // convert val from Array to object  let valToObj = {}; val.forEach((v, i) => {   valToObj[i] = v; }); val = valToObj; }    let newPrefex = prefex + key;  if (val instanceof Object) { urlParams += toUrlParams(val, newPrefex + '-'); } else { urlParams += newPrefex + '=' + val; } urlParams += '&'; } // remove last & urlParams = urlParams.slice(0, -1); // return url params return urlParams; } // encodeURI return encodeURI(toUrlParams(obj)); } // convert url params to obj function urlParamsToObj(urlParams) {  // decodeURI urlParams = decodeURI(urlParams);  let toObj = (urlParams) => { let obj = {}; let urlParamsArr = urlParams.split('&'); let subUrlParramsObj = {}; // loop through urlParams for (let i = 0; i < urlParamsArr.length; i++) { let item = urlParamsArr[i]; // get key and value let key = item.split('=')[0]; let val = item.split('=')[1] ?? null; let keys = key.split('-');  if (val == "null") { val = null; } else if (val == "undefined") { val = undefined; } else if (val == "true") { val = true; } else if (val == "false") { val = false; } else if (val == "NaN") { val = NaN; } else if (val == "Infinity") { val = Infinity; }  // if keys length is 1 then set obj[key] to val if (keys.length == 1) { // check if obj contains key if (obj.hasOwnProperty(key)) { // if obj[key] is an array then push val if (obj[key] instanceof Array) { obj[key].push(val); } else { // create array and push val obj[key] = [obj[key], val]; } } else { obj[key] = val; } } // if keys length is 2 then set obj[keys[0]][keys[1]] to val else if (keys.length > 1) { let key0 = keys[0]; // check if subUrlParramsObj contains keys[0] if (!subUrlParramsObj[key0]) { subUrlParramsObj[key0] = []; } // remove keys[0] from keys keys.shift(); // join keys with - key = keys.join('-'); let param = key + '=' + val; // add param to subUrlParramsObj[keys[0]] subUrlParramsObj[key0].push(param); } } // loop through subUrlParramsObj for (let key in subUrlParramsObj) { // join subUrlParramsObj[key] with & let val = subUrlParramsObj[key].join('&'); // set obj[key] to val obj[key] = toObj(val); } // return obj return obj; } return checkIfObjShouldBeArrayAndConvert(toObj(urlParams)); } // check if object should be converted to array, if its keys are numbers function checkIfObjShouldBeArrayAndConvert(obj) { // if obj is an array if (obj instanceof Array) { // loop through obj obj.forEach((item, i) => { // check if item is an object if (item instanceof Object) { // convert item to array obj[i] = checkIfObjShouldBeArrayAndConvert(item); } }); // return obj return obj; } // check if all keys are numbers let canConvertToArray = true; for (let key in obj) { // get value let val = obj[key];  // check if value is an object or Array if (val instanceof Object || val instanceof Array) { obj[key] = checkIfObjShouldBeArrayAndConvert(val); } if (isNaN(key)) { canConvertToArray = false; } } // order obj by keys let orderedObj = {}; Object.keys(obj).sort().forEach(function(key) { orderedObj[key] = obj[key]; }); // check if the first key is 0 if (Object.keys(orderedObj)[0] != 0) { canConvertToArray = false; } // check if the keys step is 1 let keys = Object.keys(orderedObj); // loop through keys for (let i = 0; i < keys.length - 1; i++) { // get key let key = keys[i]; // get next key let nextKey = keys[i + 1]; // get key step let keyStep = nextKey - key; // check if key step is 1 if (keyStep != 1) { canConvertToArray = false; break; } } // if all keys are numbers then convert obj to array if (canConvertToArray) { let arr = []; for (let key in orderedObj) { arr.push(orderedObj[key]); } return arr; } // return obj return obj; } // add params to url function addParamsToUrl(params, url = window.location.href) { // check if url has params if (url.indexOf('?') == -1) { url += '?'; } else { url += '&'; } return url + params ?? ''; } function addObjToUrl(obj, url = window.location.href) { return addParamsToUrl(objToUrlParams(obj), url); } // extract params from url function extractParamsFromUrl(url = window.location.href) { return urlParamsToObj(url.split('?')[1]); } // test let urlParams = objToUrlParams({ a: 1, b: "zad", c: { d: 2, e: "f" }, j: [1, 2, 3, 4]  }); console.log(urlParams); // a=1&b=zad&c-d=2&c-e=f&j=1&j=2&j=3&j=4 let obj = urlParamsToObj(urlParams); console.log(obj); // { "a": "1", "b": "zad", "j": [ "1", "2", "3", "4" ], "c": { "d": "2", "e": "f" } }
Source Link

You can use those tow functions, but make sur you don't use '-' on your object keys.

// convert obj to url params function objToUrlParams(obj) { let toUrlParams = (obj, prefex = '') => { // create url params let urlParams = ""; // loop through obj for (let key in obj) { let val = obj[key]; // if val is an object then call toUrlParams if (val instanceof Array) { val.forEach((item, index) => { urlParams += prefex + key + '=' + item + '&'; }); urlParams = urlParams.slice(0, -1); } else if (val instanceof Object) { urlParams += toUrlParams(val, key + '-'); } else { urlParams += prefex + key + '=' + val; } urlParams += '&'; } // remove last & urlParams = urlParams.slice(0, -1); // return url params return urlParams; } // encodeURI return encodeURI(toUrlParams(obj)); } // convert url params to obj function urlParamsToObj(urlParams) { let toObj = (urlParams) => { let obj = {}; let urlParamsArr = urlParams.split('&'); let subUrlParramsObj = {}; // loop through urlParams urlParamsArr.forEach((item) => { // get key and value let key = item.split('=')[0]; let val = item.split('=')[1]; let keys = key.split('-'); // if keys length is 1 then set obj[key] to val if (keys.length == 1) { // check if obj contains key if (obj.hasOwnProperty(key)) { // if obj[key] is an array then push val if (obj[key] instanceof Array) { obj[key].push(val); } else { // create array and push val obj[key] = [obj[key], val]; } } else { obj[key] = val; } } // if keys length is 2 then set obj[keys[0]][keys[1]] to val else if (keys.length > 1) { let key0 = keys[0]; // check if subUrlParramsObj contains keys[0] if (!subUrlParramsObj[key0]) { subUrlParramsObj[key0] = []; } // remove keys[0] from keys keys.shift(); // join keys with - key = keys.join('-'); let param = key + '=' + val; // add param to subUrlParramsObj[keys[0]] subUrlParramsObj[key0].push(param); } }); // loop through subUrlParramsObj for (let key in subUrlParramsObj) { // join subUrlParramsObj[key] with & let val = subUrlParramsObj[key].join('&'); // set obj[key] to val obj[key] = toObj(val); } // return obj return obj; } return toObj(urlParams); } // test let urlParams = objToUrlParams({a:1,b:"zad",c:{d:2,e:"f"},j:[1,2,3,4]}); console.log(urlParams); // a=1&b=zad&c-d=2&c-e=f&j=1&j=2&j=3&j=4 let obj = urlParamsToObj(urlParams); console.log(obj); // { "a": "1", "b": "zad", "j": [ "1", "2", "3", "4" ], "c": { "d": "2", "e": "f" } }