更直观的重新排列阵列的方法



>我正在尝试在JavaScript中创建一个重新排列数组索引的函数。我想出了以下想法,但是使用此方法,我必须为数组的每个可能长度创建一个新的else if子句,作为方法的第四个参数reformatArray。是否可以以更直观的方式添加方法中的参数?

法典:

function start() {
    var array1 = ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6", "Item 7", "Item 8"];
    reformatArray(array1, 1, 2, [1, 2, 0]);
    //output should be ["Item 1", "Item 3", "Item 4", "Item 2", "Item 4", "Item 5", "Item 6", "Item 7", "Item 8"] 
    //WORKS
    var array2 = ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6", "Item 7", "Item 8"];
    reformatArray(array2, 2, 5, [3, 1, 3, 2]);
    //output should be ["Item 1", "Item 2", "Item 6", "Item 4", "Item 6", "Item 5", "Item 8"] 
    //DOES NOT WORK because array as fourth argument is greater than 3 in length
}
function reformatArray(array, startIndex, numOfIndicesToReplace, newIndicesPositions) {
    var newPosLength = newIndicesPositions.length;
    if (newPosLength == 0) {
        array.splice(startIndex, numOfIndicesToReplace);
    } else if (newPosLength == 1) {
        array.splice(startIndex, numOfIndicesToReplace, array[startIndex + newIndicesPositions[0]]);
    } else if (newPosLength == 2) {
        array.splice(startIndex, numOfIndicesToReplace, array[startIndex + newIndicesPositions[0]], array[startIndex + newIndicesPositions[1]]);
    } else if (newPosLength == 3) {
        array.splice(startIndex, numOfIndicesToReplace, array[startIndex + newIndicesPositions[0]], array[startIndex + newIndicesPositions[1]], array[startIndex + newIndicesPositions[2]]);
    }
    //etc.
}

提前谢谢。

您可以创建要传递给拼接的参数列表,并使用 Function.prototype.apply 将它们传递给拼接。

function reformatArray(array, startIndex, numOfIndicesToReplace, newIndicesPositions) {
  var newPosLength = newIndicesPositions.length;
  var params = [startIndex, numOfIndicesToReplace];
  newIndicesPositions.forEach(function(val, pos) {
    params.push(array[startIndex + newIndicesPositions[pos]])
  });
  array.splice.apply(array, params);
}

使用 apply 。 第一个参数是绑定到this的参数,第二个参数是一系列参数。

array.splice.apply(null, arrayOfArguments)

将参数作为对象。这有助于扩展参数计数。

function reformatArray(params) {
    var array = params.array;
    var startIndex = params.startIndex;
    var numOfIndicesToReplace = params.numOfIndicesToReplace;
    var newIndicesPositions = params.newIndicesPositions;
    var newPosLength = newIndicesPositions.length;
    if (newPosLength == 0) {
        array.splice(startIndex, numOfIndicesToReplace);
    } else if (newPosLength == 1) {
    array.splice(startIndex, numOfIndicesToReplace, array[startIndex +      newIndicesPositions[0]]);
    } else if (newPosLength == 2) {
        array.splice(startIndex, numOfIndicesToReplace, array[startIndex + newIndicesPositions[0]], array[startIndex + newIndicesPositions[1]]);
    } else if (newPosLength == 3) {
        array.splice(startIndex, numOfIndicesToReplace, array[startIndex + newIndicesPositions[0]], array[startIndex + newIndicesPositions[1]], array[startIndex + newIndicesPositions[2]]);
    }
     //etc.
 }

或者,可以在方法中使用参数对象。参数是一个关键字。arguments 对象是一个类似数组的对象,对应于传递给函数的参数。有关参数关键字语法的更多信息,请查看 MDN 网站。

这是另一种方法,您可以通过拼接数组本身然后连接数组来做到这一点

function reformatArray2(array, startIndex, numOfIndicesToReplace, newIndicesPositions) {        
    var replaceValues = newIndicesPositions.map(function(index) {
      return array[startIndex + index];
    })
    array.splice(startIndex, numOfIndicesToReplace, replaceValues);
    return [].concat.apply([], array);
}

普伦克演示

(数组扁平化技术在这里找到)

最新更新