我试图在JavaScript中创建一个重新排列数组索引的函数。
我提出了以下想法,但是使用这种方法我必须为方法reformatArray
的第四个参数提供一个新的if if子句,用于每个可能的数组长度。
可以更直观的方式添加方法中的参数吗?
代码:
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.
}
提前致谢。
答案 0 :(得分:1)
您可以创建要传递给拼接的参数列表,并使用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);
}
答案 1 :(得分:0)
使用apply
。第一个参数是this
的约束,第二个是参数数组。
array.splice.apply(null, arrayOfArguments)
答案 2 :(得分:0)
将参数作为对象。这有助于扩展参数计数。
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对象。参数是一个关键字。 arguments对象是一个类似于Array的对象,对应于传递给函数的参数。有关Arguments关键字语法的更多信息,请查看MDN网站。
答案 3 :(得分:0)
这是另一种方法,你可以通过拼接数组本身然后连接数组来实现它
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);
}
(数组展平技术found here)