Skip to content

Instantly share code, notes, and snippets.

@GuyMograbi
Last active October 30, 2015 05:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GuyMograbi/d12a9fc5b09552284135 to your computer and use it in GitHub Desktop.
Save GuyMograbi/d12a9fc5b09552284135 to your computer and use it in GitHub Desktop.
turn table to object -
(function(){
var rows = _.map($('table tr'), function(r){
return _.map($(r).find('td'),function(c){
var $c = $(c);
if ( $c.children().length > 0){ // get only visible text
return $(c).find('*:not(:has(*)):visible').text();
}else{ // no children.. get text
return $(c).text();
}
});
});
var header = rows[0];
rows.splice(0,1);
var objs = _.map(rows, function(r){ // convert each row to object
var me = {};
_.each(header,function(h,index){
me[h.toLowerCase()] = r[index].trim();
});
return me;
});
return objs;
})()
/**
Use it like so:
browser.executeScript(getAllAttributes, 'table').then(...)
**/
function tableToJson( selector ){
var rows = _.map($(selector).find('tr'), function(r){
return _.map($(r).find('td'),function(c){
var $c = $(c);
if ( $c.children().length > 0){ // get only visible text
return $(c).find('*:not(:has(*)):visible').text();
}else{ // no children.. get text
return $(c).text();
}
});
});
var header = rows[0];
rows.splice(0,1);
var objs = _.map(rows, function(r){ // convert each row to object
var me = {};
_.each(header,function(h,index){
me[h.toLowerCase()] = r[index].trim();
});
return me;
});
return objs;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment