JavaScript - Converting URL like string params to an array -


i have string this:

var str = 'my_type_1=ssd&my_value_1=16gb&my_category_1=disk capacity&my_type_2=sony &my_value_2=ps4&my_category_2=console&my_roworder=2,1';  string has 3 parts except last key: part 1 -> - common prefix part 2 -> type or value or category , can keep changing part 3 -> it's numeric value binding part 1, part 2 , part 3 spreadsheet row. 

the last key called my_roworder , it's comma delimeted value. specifies how construct output array.

in above example, 2,1 means key value pair of my_type_2=sony&my_value_2=ps4&my_category_2=console should first in output array.

using javascript, parse string , create array out of it, such output is:

array (     [ 0 ] => array         (             [type] => sony             [value] => ps4             [category] => console             [row] => 2         )      [ 1 ] => array         (             [type] => ssd             [value] => 16gb             [category] => disk capacity             [row] => 1         )   ) 

how can this? partially able way:

function stringtoarray(string) {           var request = {};           var pairs = string.split('&');           (var = 0; < pairs.length-1; i++) {             var pair = pairs[i].split('=');             request[decodeuricomponent(pair[0])] = decodeuricomponent(pair[1]);           }           //i think in right track, need assistance     } 

your example output uses associative arrays, javascript doesn't have, can use array of objects instead.

this example outputs array of objects, in order specified roworder parameter. trims prefix (defined prefix), , trims row number end of key.

this work parameters in order - e.g. can mix them , parse necessary, , roworder parameter can appear anywhere in string (doesn't have @ end).

demo

function stringtoarray(string) {     var prefix = 'my_'; // set prefix     var output = [], request = [];     var pairs = string.split('&');     var order;     (var = 0; < pairs.length; i++) {         var pair = pairs[i].split('=');          if (pair[0].replace(prefix, '') == 'roworder') {             order = pair[1];         } else {             var key = decodeuricomponent(pair[0]);             var pos = key.lastindexof('_');             var trimmedkey = key.substring(0, pos).replace(prefix, '');             var row = key.substring(pos + 1);             var value = decodeuricomponent(pair[1]);             var found = false;              (var j = 0; j < output.length; j++) {                 if (output[j].row == row) {                     output[j][trimmedkey] = value;                     found = true;                 }             }              if (!found) {                 var obj = { 'row': row };                 obj[trimmedkey] = value;                 output.push(obj);             }         }     }     // ordering based on roworder parameter     var orderlist = order.split(",");     for(var k=0; k<orderlist.length; k++){         for(var l=0; l<output.length; l++){             if(output[l].row == orderlist[k]){                 request.push(output[l]);                 break;             }         }     }      return request; } 

outputs array of objects in order specified my_roworder parameter:

[     {         row: "2",         type: "sony",         value: "ps4",         category: "console"     },     {         row: "1",         type: "ssd",         value: "16gb",         category: "disk capacity"     } ] 

Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -