If you'd like a version on npm, array-move is the closest to this answer, although it's not the same implementation.(如果您想在npm上使用一个版本, array-move是最接近此答案的方法,尽管它的实现方式不同。)
See its usage section for more details.(有关更多详细信息,请参见其用法部分。) The previous version of this answer (that modified Array.prototype.move) can be found on npm at array.prototype.move .(可以在npm的array.prototype.move上找到此答案的先前版本(修改后的Array.prototype.move)。)
I had fairly good success with this function:(使用此功能,我取得了相当不错的成功:)
function array_move(arr, old_index, new_index) { if (new_index >= 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));
Note that the last return
is simply for testing purposes: splice
performs operations on the array in-place, so a return is not necessary.(请注意,最后一次return
只是出于测试目的: splice
对数组就地执行操作,因此不需要返回。)
By extension, this move
is an in-place operation.(通过扩展, move
是就地操作。) If you want to avoid that and return a copy, use slice
.(如果要避免这种情况并返回副本,请使用slice
。)
Stepping through the code:(逐步执行代码:)
- If
new_index
is greater than the length of the array, we want (I presume) to pad the array properly with new undefined
s.(如果new_index
大于数组的长度,我们(我想)要使用新的undefined
正确填充数组。) This little snippet handles this by pushing undefined
on the array until we have the proper length.(这个小片段通过在数组上推送undefined
直到我们拥有适当的长度来处理此问题。)
- Then, in
arr.splice(old_index, 1)[0]
, we splice out the old element.(然后,在arr.splice(old_index, 1)[0]
,我们拼接出旧元素。) splice
returns the element that was spliced out, but it's in an array.(splice
返回被拼接的元素,但是它在数组中。) In our above example, this was [1]
.(在我们上面的示例中,这是[1]
。) So we take the first index of that array to get the raw 1
there.(因此,我们采用该数组的第一个索引来获取原始1
。)
- Then we use
splice
to insert this element in the new_index's place.(然后,我们使用splice
在new_index的位置插入此元素。) Since we padded the array above if new_index > arr.length
, it will probably appear in the right place, unless they've done something strange like pass in a negative number.(由于如果new_index > arr.length
填充了上面的数组,除非它做了一些奇怪的事情,例如传递负数,否则它可能会出现在正确的位置。)
A fancier version to account for negative indices:(解释负指数的更高级的版本:)
function array_move(arr, old_index, new_index) { while (old_index < 0) { old_index += arr.length; } while (new_index < 0) { new_index += arr.length; } if (new_index >= 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));
Which should account for things like array_move([1, 2, 3], -1, -2)
properly (move the last element to the second to last place).(哪个应该正确地解决诸如array_move([1, 2, 3], -1, -2)
(将最后一个元素移动到倒数第二个位置)。)
Result for that should be [1, 3, 2]
.(结果应为[1, 3, 2]
。)
Either way, in your original question, you would do array_move(arr, 0, 2)
for a
after c
.(无论哪种方式,在你原来的问题,你会怎么做array_move(arr, 0, 2)
为a
后c
。)
For d
before b
, you would do array_move(arr, 3, 1)
.(对于d
在b
之前,您可以执行array_move(arr, 3, 1)
。) 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…