Javascript 如何在数组的开头和结尾附加项?
我正在创建如下文本文件:Javascript 如何在数组的开头和结尾附加项?,javascript,arrays,coffeescript,underscore.js,Javascript,Arrays,Coffeescript,Underscore.js,我正在创建如下文本文件: ["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 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 </p>↵↵", "Experiment↵↵<p>There was no one in the streets. I … realized it was a fireplace. </p><p>"</p>↵↵", "Untitled↵↵↵↵"]
["<html>...", "Chapter 1↵↵<p>As I made my way uphill...]
file.attachHeader
file.attachFooter
共同脚本:
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 </p>↵↵", "Experiment↵↵<p>There was no one in the streets. I … realized it was a fireplace. </p><p>"</p>↵↵", "Untitled↵↵↵↵"]
["<html>...", "Chapter 1↵↵<p>As I made my way uphill...]
file.attachHeader
file.attachFooter
最简单的方法是什么(在CoffeScript、JS或下划线.JS中)?您可以使用推送和取消移动
var a=["b","c"];
a.push("d");
a.unshift("a");
alert(a);