4
\$\begingroup\$
table_with_headers = $(this.dom.table).find("thead tr th").map(->
  $(this).text()
).get().join("\t")

Can one write it nicer? I especially don't like the inner function in map syntax enforced by CoffeeScript and jQuery.

\$\endgroup\$

2 Answers 2

6
\$\begingroup\$

CoffeeScript is not my forte, however I quite like this approach:

elements =  $(this.dom.table).find("thead tr th")
table_with_headers2 = ($(element).text() for element in elements ).join('\t')

This way you call out which elements you are going to process, and then use a list comprehension. In a style that is closer to JS than true CoffeeScript.

\$\endgroup\$
6
\$\begingroup\$

You can use explicit find like so

$ 'thead tr th', @dom.table

and put some indentations instead of braces

table_with_headers = $ "thead tr th", @dom.table
.map -> @.text()
.join '\t'

Also try to use list comprehesion

table_with_headers = [
  $(th).text()
  for th in
  $ "thead tr th", @dom.table
].join '\t'
\$\endgroup\$
4
  • \$\begingroup\$ sorry, but proposed solution does not work. \$\endgroup\$
    – andilabs
    Commented Oct 1, 2014 at 12:07
  • \$\begingroup\$ @andi check updates \$\endgroup\$
    – outoftime
    Commented Oct 1, 2014 at 12:21
  • \$\begingroup\$ May I suggest renaming elem to th? \$\endgroup\$ Commented Oct 1, 2014 at 17:54
  • \$\begingroup\$ @200_success, done. \$\endgroup\$
    – outoftime
    Commented Oct 3, 2014 at 13:39

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.