我需要删除 arguments
对象中的第一项,以便我的 let args
变量等于以下所有参数。
我该怎么做?
function destroyer(arr) {
let myArr = arguments[0];
let args = arguments;
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
最佳答案
如果可以的话,使用 slice
检索第一个参数之后的参数,并在函数参数中使用剩余参数而不是单个 arr
- 许多 linter 建议不要使用参数
,这里不需要使用该关键字:
function destroyer(...args) {
const otherArgs = args.slice(1);
console.log('length: ' + otherArgs.length, 'items: ' + otherArgs);
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
如果您还想引用第一个参数,请在将第一个 arr
收集到变量中后使用剩余参数:
function destroyer(arr, ...otherArgs) {
console.log('arr: ' + arr);
console.log('length: ' + otherArgs.length, 'items: ' + otherArgs);
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
关于javascript - 如何删除参数对象中的第一个参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52586717/