在使用forEach循环遍历未命名数组时,有没有办法访问目标对象的length属性?
# I'd like to be able to do something like:
[1, 2, 3].forEach (n, i) -> console.log n is < (arr.length - 1)
在使用forEach循环遍历未命名数组时,有没有办法访问目标对象的length属性?
# I'd like to be able to do something like:
[1, 2, 3].forEach (n, i) -> console.log n is < (arr.length - 1)
Array.forEach
的回调函数接受三个参数:值、索引和被遍历的数组。
因此,你可以这样做:
[1, 2, 3].forEach (n, i, thearray) -> console.log n is < (thearray.length - 1)
Javascript:
[1, 2, 3].forEach(function(n, i, thearray) {
console.log(n < thearray.length - 1);
});
getSomething().forEach...
这样的代码。 - Arnaud Le Blanc