咖啡脚本数组,对象

时间:2015-09-01 19:31:59

标签: meteor coffeescript markdown

# String of Markdown in DB
beforeMark = @content

# Render string of markdown to html string
afterMark = marked(beforeMark)

# Parse the html to HTML to extract 0, 2, 4th children node (elements)
finalMark = $.parseHTML(afterMark)

# Help needed HERE
# Get 0 2 4th elements if they exist.
# ex) if array has 4 keys, return 0, 2th
#     if array has 7 keys, return 0, 2, 4th
#     if array has 3 keys, return 0, 2th
#     if array has 1 key, return 0th
stringMark = $(finalMark[0]).prop('outerHTML') + $(finalMark[2]).prop('outerHTML') + $(finalMark[2]).prop('outerHTML')

我有上面提到的coffeescript,用于将markdown字符串截断为3个元素的html。

我需要最后一部分更高效和正确,这样它才能返回数组的0,2,4个键,但前提是它们存在。

我是新来的咖啡,我需要帮助!!

1 个答案:

答案 0 :(得分:2)

如果我理解正确的问题,我相信你想要这样的事情:

stringMark = ''
for data, index in finalMark when index in [0, 2, 4]
  stringMark += data.prop 'outerHTML'

或者如果你喜欢高尔夫码:<​​/ p>

stringMark = (v.prop 'outerHTML' for v, i in finalMark when i in [0, 2, 4]).join ''