title: Terse way to intersperse element between all elements in JavaScript array?
body: <p>Say I have an array <code>var arr = [1, 2, 3]</code>, and I want to separate each element by an element eg. <code>var sep = "&amp;"</code>, so the output is <code>[1, "&amp;", 2, "&amp;", 3]</code>.</p>
  
  <p>Another way to think about it is I want to do Array.prototype.join (<code>arr.join(sep)</code>) without the result being a string (because the elements and separator I am trying to use are Objects, not strings).</p>
  
  <p>Is there a functional/nice/elegant way to do this in either es6/7 or lodash without something that feels clunky like:</p>
  
  <pre><code>_.flatten(arr.map((el, i) =&gt; [el, i &lt; arr.length-1 ? sep : null])) // too complex
  </code></pre>
  
  <p>or </p>
  
  <pre><code>_.flatten(arr.map(el =&gt; [el, sep]).slice(0,-1) // extra sep added, memory wasted
  </code></pre>
  
  <p>or even </p>
  
  <pre><code>arr.reduce((prev,curr) =&gt; { prev.push(curr, sep); return prev; }, []).slice(0,-1)
  // probably the best out of the three, but I have to do a map already
  // and I still have the same problem as the previous two - either
  // inline ternary or slice
  </code></pre>
  
  <p>Edit: Haskell has this function, called <a href="http://zvon.org/other/haskell/Outputlist/intersperse_f.html">intersperse</a></p>