Last active
October 30, 2015 05:55
-
-
Save GuyMograbi/d12a9fc5b09552284135 to your computer and use it in GitHub Desktop.
turn table to object -
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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; | |
})() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
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