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

时间:2015-06-01 22:21:47

标签: 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 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]);

并没有完全给我我想要的东西

5 个答案:

答案 0 :(得分:1)

这里使用原型在特定数字后插入数组:

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( ... )第一个toMove的值清除数组。在特定数字后插入第二个toMove

function moveAfter(arr, toMove, after) {
    toMove.forEach(function (value) {
        arr.splice(arr.indexOf(value), 1);
    });
    var res = arr.insertIntoArr(toMove, after);
    return res;
}

Example

答案 1 :(得分:0)

这样的事情:http://plnkr.co/edit/k2h6BWTUCFj5BS4oFF8C

(function(){
  var arr = [0,1,2,3,4,5,6,7,8,9];
  var userArr = [4,5,7];
  var temp = [];
  var pos = 1;

  for(var i = arr.length; i >= 0; i--){
    if(userArr.indexOf(arr[i]) !== -1){
      temp.push(arr[i]);
      arr.splice(i, 1);
    }
  }

  for(var i = 0; i < temp.length; i++){
    arr.splice(arr.indexOf(pos) + 1, 0, temp[i]);
  }

  console.log(arr);
  //outputs [0, 1, 4, 5, 7, 2, 3, 6, 8, 9]
})();

答案 2 :(得分:0)

使用您的想法

private string determineSearchParameter()
{
    //Execute statements if ALL textboxes are empty
    if (string.IsNullOrWhiteSpace(textbox1.Text) && string.IsNullOrWhiteSpace(textbox2.Text) &&
        string.IsNullOrWhiteSpace(textbox3.Text))
    {
        return "Please Enter a Search Parameter";
    }
    //Execute all fields have an input
    if (!string.IsNullOrEmpty(textbox2.Text) && !string.IsNullOrWhiteSpace(textbox1.Text) &&
        !string.IsNullOrWhiteSpace(textbox3.Text))
    {
        return "Please only enter one Criteria";
    }
    //Execute statments if multiple textboxes have values
    if (!string.IsNullOrWhiteSpace(textbox3.Text) && !string.IsNullOrWhiteSpace(textbox1.Text))
    {
        return "Please only enter one Criteria";
    }

    if (!string.IsNullOrWhiteSpace(textbox3.Text) && !string.IsNullOrWhiteSpace(textbox2.Text))
    {
        return "Please only enter one Criteria";
    }
    if (!string.IsNullOrWhiteSpace(textbox1.Text) && !string.IsNullOrWhiteSpace(textbox2.Text))
    {
        return "Please only enter one Criteria";
    }
    if (!string.IsNullOrWhiteSpace(textbox1.Text) && string.IsNullOrEmpty(textbox2.Text) &&
        string.IsNullOrEmpty(textbox3.Text))
    {
        return "Something else";
    }
    if (!string.IsNullOrWhiteSpace(textbox2.Text) && string.IsNullOrEmpty(textbox1.Text) &&
        string.IsNullOrEmpty(textbox3.Text))
    {
        return "Something there";
    }
    if (!string.IsNullOrWhiteSpace(textbox3.Text) && string.IsNullOrEmpty(textbox1.Text) &&
        string.IsNullOrEmpty(textbox2.Text))
    {
        return "Something here";
    }
    return "";
}

然后你使用

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;
}

希望它有效,

答案 3 :(得分:0)

试试这个:

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;
};

答案 4 :(得分:0)

这样的东西?

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;
}