从一个阵列位置移动一个数组元素到另一个 [英] Move an array element from one array position to another

查看:119
本文介绍了从一个阵列位置移动一个数组元素到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很难搞清楚如何将一个数组元素。例如,给定以下

I'm having a hard time figuring out how to move an array element. For example, given the following:

var arr = [ 'a', 'b', 'c', 'd', 'e'];

我如何编写一个函数来移动D'B'

'A''C'

在移动中,元件的其余部分的索引应该更新。这意味着在第一个例子中的移动ARR [0]将='一个'之后,改编[1] ='D'常用3 [2] ='B',ARR [3] ='c'的,ARR [4] = E

After the move, the indices of the rest of the elements should be updated. This means in the first example after the move arr[0] would = 'a', arr[1] = 'd' arr[2] = 'b', arr[3] = 'c', arr[4] = 'e'

这似乎应该是pretty简单,但我不能换我的头周围。

This seems like it should be pretty simple, but I can't wrap my head around it.

推荐答案

我有这个功能相当不错的成功:

I had fairly good success with this function:

Array.prototype.move = function (old_index, new_index) {
    if (new_index >= this.length) {
        var k = new_index - this.length;
        while ((k--) + 1) {
            this.push(undefined);
        }
    }
    this.splice(new_index, 0, this.splice(old_index, 1)[0]);
    return this; // for testing purposes
};

举例code: [1,2,3] .move(0,1) [2,1,3]

请注意,最后收益只是用于测试目的:<一href=\"https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/splice\"><$c$c>splice就地阵列上进行操作,因此一回是没有必要的。推而广之,这个移动是就地操作。如果你想避免和返回副本,使用<一个href=\"https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/slice\"><$c$c>slice.

Note that the last return is simply for testing purposes: splice performs operations on the array in-place, so a return is not necessary. By extension, this move is an in-place operation. If you want to avoid that and return a copy, use slice.

通过code步进:


  1. 如果 NEW_INDEX 比数组的长度时,我们希望(我presume)垫阵列妥善处理新的未定义秒。这个小片断按下未定义阵列上,直到我们有适当的长度处理这一点。

  2. 然后,在 this.splice(old_index,1)[0] ,我们拼接出旧元素。 拼接返回被拼接出的元素,但它是在数组中。在上面的例子,这是 [1] 。因此,我们采取数组的第一个指数,以获得原始的 1 那里。

  3. 然后我们使用拼接插入在NEW_INDEX的地方这个元素。由于我们上面填充的数组,如果 NEW_INDEX&GT; this.length ,它可能会出现在正确的地方,除非他们已经做了一些奇怪的像传递一个负数。

  1. If new_index is greater than the length of the array, we want (I presume) to pad the array properly with new undefineds. This little snippet handles this by pushing undefined on the array until we have the proper length.
  2. Then, in this.splice(old_index, 1)[0], we splice out the old element. splice returns the element that was spliced out, but it's in an array. In our above example, this was [1]. So we take the first index of that array to get the raw 1 there.
  3. Then we use splice to insert this element in the new_index's place. Since we padded the array above if new_index > this.length, it will probably appear in the right place, unless they've done something strange like pass in a negative number.

一个票友版本占负指数:

A fancier version to account for negative indices:

Array.prototype.move = function (old_index, new_index) {
    while (old_index < 0) {
        old_index += this.length;
    }
    while (new_index < 0) {
        new_index += this.length;
    }
    if (new_index >= this.length) {
        var k = new_index - this.length;
        while ((k--) + 1) {
            this.push(undefined);
        }
    }
    this.splice(new_index, 0, this.splice(old_index, 1)[0]);
    return this; // for testing purposes
};

哪些应占像物[1,2,3,4,5] .move(-1,-2)正确(移动到的最后一个元件倒数第二位)。结果为,应该是 [1,2,3,5,4]

Which should account for things like [1, 2, 3, 4, 5].move(-1, -2) properly (move the last element to the second to last place). Result for that should be [1, 2, 3, 5, 4].

无论哪种方式,在你原来的问题,你会怎么做 arr.move(0,2) A C 。对于 D B ,你会做 arr.move(3,1)

Either way, in your original question, you would do arr.move(0, 2) for a after c. For d before b, you would do arr.move(3, 1).

这篇关于从一个阵列位置移动一个数组元素到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆