Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you'd like a version on npm, <a href="https://www.npmjs.com/package/array-move" rel="noreferrer">array-move</a> is the closest to this answer, although it's not the same implementation. See its usage section for more details. The previous version of this answer (that modified Array.prototype.move) can be found on npm at <a href="https://www.npmjs.com/package/array.prototype.move" rel="noreferrer">array.prototype.move</a>.</p> <hr> <p>I had fairly good success with this function:</p> <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code"> <pre class="snippet-code-js lang-js prettyprint-override"><code>function array_move(arr, old_index, new_index) { if (new_index &gt;= arr.length) { var k = new_index - arr.length + 1; while (k--) { arr.push(undefined); } } arr.splice(new_index, 0, arr.splice(old_index, 1)[0]); return arr; // for testing }; // returns [2, 1, 3] console.log(array_move([1, 2, 3], 0, 1)); </code></pre> </div> </div> </p> <p>Note that the last <code>return</code> is simply for testing purposes: <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/splice" rel="noreferrer"><code>splice</code></a> performs operations on the array in-place, so a return is not necessary. By extension, this <code>move</code> is an in-place operation. If you want to avoid that and return a copy, use <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/slice" rel="noreferrer"><code>slice</code></a>.</p> <p>Stepping through the code:</p> <ol> <li>If <code>new_index</code> is greater than the length of the array, we want (I presume) to pad the array properly with new <code>undefined</code>s. This little snippet handles this by pushing <code>undefined</code> on the array until we have the proper length.</li> <li>Then, in <code>arr.splice(old_index, 1)[0]</code>, we splice out the old element. <code>splice</code> returns the element that was spliced out, but it's in an array. In our above example, this was <code>[1]</code>. So we take the first index of that array to get the raw <code>1</code> there.</li> <li>Then we use <code>splice</code> to insert this element in the new_index's place. Since we padded the array above if <code>new_index &gt; arr.length</code>, it will probably appear in the right place, unless they've done something strange like pass in a negative number. </li> </ol> <p>A fancier version to account for negative indices: </p> <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code"> <pre class="snippet-code-js lang-js prettyprint-override"><code>function array_move(arr, old_index, new_index) { while (old_index &lt; 0) { old_index += arr.length; } while (new_index &lt; 0) { new_index += arr.length; } if (new_index &gt;= arr.length) { var k = new_index - arr.length + 1; while (k--) { arr.push(undefined); } } arr.splice(new_index, 0, arr.splice(old_index, 1)[0]); return arr; // for testing purposes }; // returns [1, 3, 2] console.log(array_move([1, 2, 3], -1, -2));</code></pre> </div> </div> </p> <p>Which should account for things like <code>array_move([1, 2, 3], -1, -2)</code> properly (move the last element to the second to last place). Result for that should be <code>[1, 3, 2]</code>. </p> <p>Either way, in your original question, you would do <code>array_move(arr, 0, 2)</code> for <code>a</code> after <code>c</code>. For <code>d</code> before <code>b</code>, you would do <code>array_move(arr, 3, 1)</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload