I created this small script to strip the data out of tables that have hyperlinks as their <th /> elements. I was hoping to get input on code clarity and possibly simplification, efficiency is not much of an issue for me as there are not any huge tables on Wikipedia.

var arr = $('tbody').children('tr').map(function(idx) {
  return(idx === 0) ?
    rows: $('th', this).map(function(idx) {
      return {
        href: $('a', this).attr('href'),
        title: $('a', this).text()
  } : {
    href: $('th a', this).attr('href'),
    title: $('th a', this).text(),
    rows: $('td', this).map(function(idx) {
      return $(this).text();

Here is an example of a table that this scraper can be run on.

  • 2
    \$\begingroup\$ Could you show an example table that this can be run on? \$\endgroup\$ Jun 19, 2015 at 21:04
  • \$\begingroup\$ @SimonAndréForsberg Added to question thanks for looking! You can try it out by running it in the developer console in any browser as wikipedia uses JQuery. \$\endgroup\$
    – Mike
    Jun 19, 2015 at 21:08

1 Answer 1


This looks pretty nice, I only have a few nit-picks, really.

What you call "rows" are actually "columns". It's a bit misleading. The rows are actually the var arr that you're making.

The ternary is a bit confusing. If clarity is important to you (as you mentioned), then I think a good old-fashioned if-else would be better.

Lastly, the idx function parameters in the inner map calls are unused, so you could just as well drop them.

  • \$\begingroup\$ The ternary is a bit confusing.. +1. Especially when it become such enormous one as in the code. BTW: else keyword doesn't even needed when we replace the ternary with if construct in this case (because of return use). \$\endgroup\$
    – hindmost
    Jun 20, 2015 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.