无法在JavaScript中的变长参数数组上运行函数

我试图对我的参数执行一个函数,该函数是可变长度的。我似乎无法在我的arguments数组上运行任何函数,包括sort。

library(data.table) # data.table 1.12.0
# small data.table example   ## edited to be reproducible 
m <- matrix(c(5,5,6,7,6),nrow = 8,ncol = 2)
dt <- setDT(as.data.frame(m)) 
dt.uniq <- unique(dt)

#this returns the ID or position of the rows in the first table on the second table
match(transpose(dt),transpose(dt.uniq))
## [1] 1 1 2 2 3 2 4 4

我收到此错误:

function findKeyFromNotes()
    {
         var notes = arguments.slice(); 
         return notes;  
    }

谢谢, 那库尔

ping00000 回答:无法在JavaScript中的变长参数数组上运行函数

在现代JavaScript中,您可以使用传播语法将所有参数收集到单个数组值中:

function findKeyFromNotes(... notes) {
  // notes will be an array
}

在“传统” JavaScript中,最好的做法是:

function findKeyFromNotes() {
  var notes = [];
  for (var i = 0; i < arguments.length; ++i) notes[i] = arguments[i];
  // now notes is a plain array
}
本文链接:https://www.f2er.com/3162988.html

大家都在问