HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Get <TH> texts into a tab separated string

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
textsintotabseparatedgetstring

Problem

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.

Solution

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'

Code Snippets

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

Context

StackExchange Code Review Q#64389, answer score: 6

Revisions (0)

No revisions yet.