访问Array.forEach中的未命名数组

pdo*_*926 5 javascript coffeescript

在未命名的数组上使用forEach循环时,有没有办法访问目标对象的长度属性?

# I'd like to be able to do something like:
[1, 2, 3].forEach (n, i) -> console.log n is < (arr.length - 1)
Run Code Online (Sandbox Code Playgroud)

Arn*_*anc 6

Array.forEach采用树参数的回调:值,索引和遍历的数组.

所以你可以这样做:

[1, 2, 3].forEach (n, i, thearray) -> console.log n is < (thearray.length - 1)
Run Code Online (Sandbox Code Playgroud)

使用Javascript:

[1, 2, 3].forEach(function(n, i, thearray) {
    console.log(n < thearray.length - 1);
});
Run Code Online (Sandbox Code Playgroud)