最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Why provide an array argument in Javascript's array.forEach callback? - Stack Overflow

programmeradmin2浏览0评论

Javascript's array iteration functions (forEach, every, some etc.) allow you to pass three arguments: the current item, the current index and the array being operated on.

My question is: what benefits are there to operating on the array as an argument, vs. accessing it via the closure?

Why should I use this:

myArray.forEach(function(item, i, arr) {doSomething(arr);});

Instead of this:

myArray.forEach(function(item, i) {doSomething(myArray);});

Javascript's array iteration functions (forEach, every, some etc.) allow you to pass three arguments: the current item, the current index and the array being operated on.

My question is: what benefits are there to operating on the array as an argument, vs. accessing it via the closure?

Why should I use this:

myArray.forEach(function(item, i, arr) {doSomething(arr);});

Instead of this:

myArray.forEach(function(item, i) {doSomething(myArray);});
Share Improve this question asked Sep 16, 2016 at 9:54 Marcus HarrisonMarcus Harrison 8696 silver badges20 bronze badges 1
  • If you want to pass array as argument, why wrap it in .forEach in the first place? – Rajesh Commented Sep 16, 2016 at 9:58
Add a comment  | 

2 Answers 2

Reset to default 22

It is possible that you want to pass a generic function as an argument to forEach and not an anonymous function. Imagine a situation where you have a function defined like that:

function my_function(item, i, arr) {
    // do some stuff here
}

and then use it on different arrays:

arr1.forEach(my_function);
arr2.forEach(my_function);

The third argument allows the function to know which array is operating on.

Another case where this might be usefull, is when the array has not been stored in a variable and therefore does not have a name to be referenced with, e.g.:

[1, 2, 3].forEach(function(item, i, arr) {});

there is many cases in real life where knowing where an error is occuring and what was the content of your array. And focusly in a backend JS context like a NodeJS application..

Imagine your website is attacked, you want to know how your pirats are looking to enter into your website, in that case providing the array which contained the entire elements is useful. Look at the ECMASCRIPT 2020 page 642-643 it is written

callbackfn is called with three arguments: 
the value of the element,
the index of the element,
and the object being traversed

illustration :

ok try this example

let myArray = [2 , 3, 5 , 7 , "ഊമ്പി", 13]
let foo = null
myArray.forEach(function(value, index, currentObject) {
    try {
        foo = 3.14*value;
        if (Number.isNaN(foo)) throw "foo is NaN" 
    } 
    catch (error) {
        console.error(error + ", and value: " + value + ", index: " + index + ", currentObject: " + currentObject);
    }
});

Hope that answer will help newbies That's all folk's !

发布评论

评论列表(0)

  1. 暂无评论