将数组元素从一个数组位置移动到另一数组位置

梦想与她 提交于 2020-02-27 16:01:21

我很难弄清楚如何移动数组元素。 例如,给出以下内容:

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

我为什么能写入移动功能'd'之前, 'b'

'a''c'

移动之后,应更新其余元素的索引。 这意味着在第一个示例中,移动后arr [0] ='a',arr [1] ='d'arr [2] ='b',arr [3] ='c',arr [4] = 'e'

这看起来应该很简单,但是我无法将其包裹住。


#1楼

您可以实现一些基本的微积分,并创建通用函数以将数组元素从一个位置移动到另一位置。

对于JavaScript,它看起来像这样:

function magicFunction (targetArray, indexFrom, indexTo) { 

    targetElement = targetArray[indexFrom]; 
    magicIncrement = (indexTo - indexFrom) / Math.abs (indexTo - indexFrom); 

    for (Element = indexFrom; Element != indexTo; Element += magicIncrement){ 
        targetArray[Element] = targetArray[Element + magicIncrement]; 
    } 

    targetArray[indexTo] = targetElement; 

}

请查看“积木”中的“移动数组元素”以获取详细说明。

http://www.gloommatter.com/DDesign/programming/moving-any-array-elements-universal-function.html


#2楼

Array.move.js

摘要

在数组中移动元素,返回包含已移动元素的数组。

句法

array.move(index, howMany, toIndex);

参量

index :元素移动的索引。 如果为负, 索引将从末尾开始。

howMany :要从索引移动的元素数。

toIndex :放置移动元素的数组的索引。 如果为负,则toIndex将从结尾开始。

用法

array = ["a", "b", "c", "d", "e", "f", "g"];

array.move(3, 2, 1); // returns ["d","e"]

array; // returns ["a", "d", "e", "b", "c", "f", "g"]

Polyfill

Array.prototype.move || Object.defineProperty(Array.prototype, "move", {
    value: function (index, howMany, toIndex) {
        var
        array = this,
        index = parseInt(index) || 0,
        index = index < 0 ? array.length + index : index,
        toIndex = parseInt(toIndex) || 0,
        toIndex = toIndex < 0 ? array.length + toIndex : toIndex,
        toIndex = toIndex <= index ? toIndex : toIndex <= index + howMany ? index : toIndex - howMany,
        moved;

        array.splice.apply(array, [toIndex, 0].concat(moved = array.splice(index, howMany)));

        return moved;
    }
});

#3楼

最后,我将这两种方法结合起来,无论在长距离还是短距离移动时,效果都更好。 我得到了相当一致的结果,但是这可能会被比我聪明的人进行一些调整,以针对不同的尺寸进行不同的工作,等等。

当移动物体较小距离时,使用其他一些方法比使用拼接要快得多(x10)。 但是,这可能会根据数组的长度而变化,但是对于大型数组而言确实如此。

function ArrayMove(array, from, to) {
    if ( Math.abs(from - to) > 60) {
        array.splice(to, 0, array.splice(from, 1)[0]);
    } else {
        // works better when we are not moving things very far
        var target = array[from];
        var inc = (to - from) / Math.abs(to - from);
        var current = from;
        for (; current != to; current += inc) {
            array[current] = array[current + inc];
        }
        array[to] = target;    
    }
}

http://jsperf.com/arraymove-many-sizes


#4楼

splice()方法在数组中添加/删除项目,并返回删除的项目。

注意:此方法更改原始数组。 / w3schools /

Array.prototype.move = function(from,to){
  this.splice(to,0,this.splice(from,1)[0]);
  return this;
};

var arr = [ 'a', 'b', 'c', 'd', 'e'];
arr.move(3,1);//["a", "d", "b", "c", "e"]


var arr = [ 'a', 'b', 'c', 'd', 'e'];
arr.move(0,2);//["b", "c", "a", "d", "e"]

由于该函数是可链接的,因此也可以工作:

alert(arr.move(0,2).join(','));

在这里演示


#5楼

我的2c。 易于阅读,有效,快速,不会创建新数组。

function move(array, from, to) {
  if( to === from ) return array;

  var target = array[from];                         
  var increment = to < from ? -1 : 1;

  for(var k = from; k != to; k += increment){
    array[k] = array[k + increment];
  }
  array[to] = target;
  return array;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!