javascript - Creating a jQuery array based on key of json array -


i have json array thats being sent ajax response.

the json array in following manner

[{"id":"11105","name":"gummy drop (iphone, free, row except cn, 72.3mb, w"},{"id":"11107","name":"gummy drop (ipad, free, row except cn, 72.3mb, w\/ "},{"id":"4274","name":"z-redirect non-targeted traffic dragon city mobile"},{"id":"6484","name":"z-redirect non-targeted traffic dragon city mobile"}] 

now, sometimes, key can id , name, can roll , address

so it's not predictable or some.

i need key form json array/object , build thing this

var acolumns = [{ stitle: "id"}, { stitle: "name" }]; 

someone suggested me piece of code, follows:

$.post("ajax.php",{'version':v,'category':ctg,'country':cnt,'func':'show_datatable'},                         function(data)                         {                             /*var acolumns = [{ stitle: "week"}, { stitle: "winkelformule" }];*/                             for(var = 0; < data.length; i++)                              {                                 keycolumns = object.keys(data[i]);                                  for(j = 0; j < keycolumns.length; j++)                                 {                                     alert(keycolumns[j]);                                     if($.inarray(keycolumns[j],acolumns.stitle)<=0)                                     {                                         acolumns.push({stitle: keycolumns[j]}) //checks if                                      }                                                                      }                             }                        },'json'); 

it seems idea right, fishy in these lines:

if($.inarray(keycolumns[j],acolumns.stitle)<=0)     {         acolumns.push({stitle: keycolumns[j]}) //checks if      } 

var acolumns = [];  data.foreach(function (el) {         object.keys(el).foreach(function (key) {         if (!~acolumns.indexof(key)) {             acolumns.push(key);         }     }) })  acolumns = acolumns.map(function (key) {     return {stitle: key}; });  console.log(acolumns); 

demo: http://jsfiddle.net/e50rwfbr/

-- line not correct

if($.inarray(keycolumns[j], acolumns.stitle)<=0) 

you need check in acolumns not keycolumns..

var acolumns = [], i, j;  for(i = 0; < data.length; i++) {     keycolumns = object.keys(data[i]);      (j = 0; j < keycolumns.length; j++) {         if (!acolumns.length || !acolumns.filter(function(el) { return el.stitle == keycolumns[j]; }).length) {             acolumns.push({stitle: keycolumns[j]});         }                                     } } 

demo: http://jsfiddle.net/e50rwfbr/2/


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -