Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a JavaScript array from URL

Tags:

javascript

url

I need to make a Javascript array from URL, eg:

turn this:

http://maps.google.com/maps/api/staticmap?center=Baker Street 221b, London&size=450x450&markers=Baker Street 221b, London&sensor=false 

Into something like:

array['center'] = Baker Street 221b, London array['size'] = 450x450 // and so on... 

I need to make this serializaion/unserialization work both ways (url to array and array to the part of the url). Are there some built-in functions that do this?

Thanks in advance!

like image 980
skazhy Avatar asked Nov 28 '10 16:11

skazhy


People also ask

How do you create an array in JavaScript?

Creating an Array Using an array literal is the easiest way to create a JavaScript Array. Syntax: const array_name = [item1, item2, ...]; It is a common practice to declare arrays with the const keyword.

How do you turn a string into an array in JavaScript?

The string in JavaScript can be converted into a character array by using the split() and Array. from() functions.

Can we send array in query string in JavaScript?

You can pass data, including arrays via the Query String when using NavigationManager to navigate to a different page in your Blazor app.

What is JavaScript array with example?

An array is an object that can store multiple values at once. For example, const words = ['hello', 'world', 'welcome']; Here, words is an array.


2 Answers

URL to array: (adapted from my answer here)

function URLToArray(url) {     var request = {};     var pairs = url.substring(url.indexOf('?') + 1).split('&');     for (var i = 0; i < pairs.length; i++) {         if(!pairs[i])             continue;         var pair = pairs[i].split('=');         request[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);      }      return request; } 

Array to URL:

function ArrayToURL(array) {   var pairs = [];   for (var key in array)     if (array.hasOwnProperty(key))        pairs.push(encodeURIComponent(key) + '=' + encodeURIComponent(array[key]));   return pairs.join('&'); } 
like image 81
casablanca Avatar answered Oct 12 '22 01:10

casablanca


the above function URLToArray is not working when url string has elem[]=23&elem[]=56.. see below the adapted function... hope it is working - not 100% tested

function URLToArray(url) {         var request = {};         var arr = [];         var pairs = url.substring(url.indexOf('?') + 1).split('&');         for (var i = 0; i < pairs.length; i++) {           var pair = pairs[i].split('=');            //check we have an array here - add array numeric indexes so the key elem[] is not identical.           if(endsWith(decodeURIComponent(pair[0]), '[]') ) {               var arrName = decodeURIComponent(pair[0]).substring(0, decodeURIComponent(pair[0]).length - 2);               if(!(arrName in arr)) {                   arr.push(arrName);                   arr[arrName] = [];               }                arr[arrName].push(decodeURIComponent(pair[1]));               request[arrName] = arr[arrName];           } else {             request[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);           }         }         return request;     } 

where endWith is taken from here

function endsWith(str, suffix) {     return str.indexOf(suffix, str.length - suffix.length) !== -1; } 
like image 24
alex toader Avatar answered Oct 12 '22 01:10

alex toader