【问题标题】:How do I attach an item at the beginning and at the end of an Array?如何在数组的开头和结尾附加一个项目?
【发布时间】:2014-06-01 17:06:47
【问题描述】:

我正在创建这样的文本文件:

咖啡:

file = for post in posts
      "#{post.title}\n\n#{post.content}\n\n"

Javascrit:

file = (function() {
  var _i, _len, _results;
  _results = [];
  for (_i = 0, _len = posts.length; _i < _len; _i++) {
    post = posts[_i];
    _results.push("" + post.title + "\n\n" + post.content + "\n\n");
  }
  return _results;
})();

输出如下:

["Chapter 1↵↵<p>As I made my way uphill, I understoo… of the woods, and continued my way uphill.</p>↵↵", "Chapter 2↵↵<p>I hadn't seen An-Mei for nearly six …pths of my heart, waiting to be awoken.</p><br>↵↵", "Chapter 3↵↵<p>"Sure they allow visitors?" I asked …ht?”</p><p>She gave her head another shake.</p>↵↵", "Chapter 4↵↵<p>Back in the hotel, I thought about w…leading the way—we ventured into the woods.</p>↵↵", "Notes↵↵<p>- Search places where only one word can be used &nbsp;</p>↵↵", "Experiment↵↵<p>There was no one in the streets. I … realized it was a fireplace.&nbsp;</p><p>"</p>↵↵", "Untitled↵↵↵↵"]

现在我想将文本文件转换为 HTML 文档,因此我想修改 file 以输出如下内容:

["<html>...", "Chapter 1↵↵<p>As I made my way uphill...]

也许做这样的事情:

file.attachHeader
file.attachFooter

最简单的方法是什么(在 CoffeScript、JS 或 Underscore.js 中)?

【问题讨论】:

    标签: javascript arrays coffeescript underscore.js


    【解决方案1】:

    您可以使用 push 和 unshift。

        var a=["b","c"];
        a.push("d");
        a.unshift("a");
        alert(a);
    

    Example

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-03
      • 2017-07-05
      • 2011-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-03
      • 2019-01-23
      相关资源
      最近更新 更多