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

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

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

我为何能写入移动功能'd'以前, 'b'数组

'a''c'app

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

这看起来应该很简单,可是我没法将其包裹住。 函数


#1楼

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

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

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; 

}

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

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


#2楼

Array.move.js

摘要

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

句法

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;
}
相关文章
相关标签/搜索