Javascript 将数组中的几个元素移动到数组中的某个索引

Javascript 将数组中的几个元素移动到数组中的某个索引,javascript,arrays,sorting,Javascript,Arrays,Sorting,我有一个元素数组 0,1,2,3,4,5,6,7,8,9 用户可以选择任意数量的元素,并要求将它们移动到任意一个特定元素之后 示例:例如,要求将4,5,7移到1之后,从而导致 0,1,4,5,7,2,3,6,8,9 或要求在9之后移动0,5 1,2,3,4,6,7,8,9,0,5 任何伪代码都非常受欢迎 move_after= function(after, move_array) { //remove elements f

我有一个元素数组

0,1,2,3,4,5,6,7,8,9

用户可以选择任意数量的元素,并要求将它们移动到任意一个特定元素之后

示例:例如,要求将4,5,7移到1之后,从而导致

0,1,4,5,7,2,3,6,8,9

或要求在9之后移动0,5

1,2,3,4,6,7,8,9,0,5

任何伪代码都非常受欢迎

                move_after= function(after, move_array) {
                    //remove elements from the array
                    move_array.forEach(function(element) {
                        var index = operations.indexOf(element);
                        operations.splice(index, 1);
                    });


                    var after_index = operations.indexOf(after) + 1;

                    //insert each move_array element to array
                    move_array.forEach(function(element) {
                         operations.splice(after_index++, 0, element);
                    });
                }

                move_after(2, [0,1]);

没有给我想要的东西

像这样的东西怎么样:

(函数(){
var arr=[0,1,2,3,4,5,6,7,8,9];
var userArr=[4,5,7];
var-temp=[];
var pos=1;
对于(变量i=arr.length;i>=0;i--){
if(userArr.indexOf(arr[i])!=-1){
温度推力(arr[i]);
阵列拼接(i,1);
}
}
对于(变量i=0;i
运用你的想法

function move_after(orig_array, after, move_array) {
                //remove elements from the array
                move_array.forEach(function(element) {
                    var index = operations.indexOf(element);
                    orig_array.splice(index, 1);
                });

                var after_index = orig_array.indexOf(after) + 1;

                //insert each move_array element to array
                move_array.forEach(function(element) {
                     orig_array.splice(after_index++, 0, element);
                });
          return orig_array;
}
然后你用

var result = move_after([0, 1, 2] , 2, [0,1]);
希望它能起作用

试试这个:

move_after = function (after, move_array) {
  var i, s;

  s = [];
  for (i = 0; i < 10; i++)
  {
    // only append "i" it if is NOT in move_array
    if (move_array.indexOf(i) === -1) s.push(i);
    if (i == after) s.push(move_array);
  }

  return s;
};
move\u after=函数(after,move\u数组){
变量i,s;
s=[];
对于(i=0;i<10;i++)
{
//仅当不在move_数组中时才附加“i”
if(move_array.indexOf(i)=-1)s.push(i);
if(i==之后)s.push(move_数组);
}
返回s;
};
像这样的东西

var a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
var b = move_after(1, [4, 5, 7]);
var c = move_after(9, [0, 5]);

console.log(a);
console.log(b);
console.log(c);

function move_after(moveAfter, toMove) {
    var arr = a.reduce(function (c, e, i) {
        if (toMove.indexOf(e) === -1) {
            c.push(e);
        }
        return c;
    }, []);
    var toMoveAfterIndex = arr.indexOf(moveAfter) + 1;
    Array.prototype.splice.apply(
        arr, [toMoveAfterIndex, 0].concat(toMove)
    );
    return arr;
}

这里使用的是原型,它将一个数组插入到一个数组中的特定数字后面:

Array.prototype.insertIntoArr = function(insert, digit) {
    var i = this.indexOf(digit) + 1;
    return this.slice(0, i).concat(insert).concat(this.slice(i));
}
function moveAfter(arr, toMove, after) {
    toMove.forEach(function (value) {
        arr.splice(arr.indexOf(value), 1);
    });
    var res = arr.insertIntoArr(toMove, after);
    return res;
}
函数moveAfter(…)
首先从
toMove
的值中清除数组。第二个
toMove
插入特定数字后:

Array.prototype.insertIntoArr = function(insert, digit) {
    var i = this.indexOf(digit) + 1;
    return this.slice(0, i).concat(insert).concat(this.slice(i));
}
function moveAfter(arr, toMove, after) {
    toMove.forEach(function (value) {
        arr.splice(arr.indexOf(value), 1);
    });
    var res = arr.insertIntoArr(toMove, after);
    return res;
}