我在这方面还很陌生,并且一直在尝试学习有关在Javascript中使用数组的更多信息。 我一直在做一些函数,将数组和新的输入粘贴到这样的函数中:
#include <node.h>
仅在函数中使用单个参数就可以了:但是当我尝试与function printArray(array){
//This function will print out each of the entries of the array
console.log("This is the Output from the Print Array Function: ")
console.time()
let arr = array;
for(let index = 0; index < arr.length; index++){
console.log(arr[index]);
//This is the function
}
console.timeEnd();
}
做类似的事情时,原始语法是:array.push()
我想这是我的问题,如何我可以在函数论点中复制它,它可以是单个输入或多个输入。这是我所做的:
array.prototype.push(input1 --> InputN)
(请原谅console.logs和时间戳的过大杀伤力。我只是将它用作学习助手。如果有人可以帮助我,将不胜感激。 非常感谢=)
答案 0 :(得分:1)
您可以将rest parameter与...
结合使用:
function nPushArray(array, ...inputs) {
//This function ADDS N ITEMS to the END OF THE ARRAY
console.time();
console.log("This is the Output from the n Push Array Function- Adding N Items to the END of the ARRAY")
console.log("This is the Original Copy of the Array: ", array);
array.push(...inputs); //This is the function
console.log("This is the Ammended Cop of the Array: ", array);
console.timeEnd();
}
//usage
nPushArray([1, 2, 3], 4, 5);
nPushArray([1, 2, 3], 4, 5, 6, 7, 8)