There is such a code

var abc = [1, 55, 399, 100].reduce(function(acc, el) { console.log('el= ' + el); acc += el; console.log('acc= ' + acc); return acc; }, 0); console.log('abc=' + abc); // el= 1 // acc= 1 // el= 55 // acc= 56 // el= 399 // acc= 455 // el= 100 // acc= 555 // abc=555 

I don’t understand how to get access to each element of the array [1, 55, 399, 100] when calling the call () method and substituting the argument, let's say +10

 var abc = [1, 55, 399, 100].reduce.call(каждый_элемент_плюс_десять,function(acc, el) {} // или по другому в аргументе call должно быть такое [1, 55, 399, 100].forEach( function(element, index) { console.log(element+10); return element+10; }); 

Is it possible to do this without creating a pre-array to increase its values, and then substitute in reduce.call ()?

Without preforming an array, I mean this:

 var b = [1, 55, 399, 100]; var d = []; b.forEach( function(element, i) { d[i] = element+10; }); document.writeln(d +'<br>'); var abc = b.reduce.call(d, function(acc, el) { acc += el; return acc; }, 0); document.writeln('abc=' + abc); 

  • The first call parameter is the context — in fact, for the reduce function — it must be an array or an ArrayLike object. Therefore, it is not entirely clear what you are trying to do after all - Grundy
  • @Grundy Is it possible to get a context from an array? - Jean-Claude
  • here it is :) confusion in terminology. Judging by the code, there is no need for a call at all, it suffices to return acc+el+10 - Grundy
  • Well, you can not get the context from anything - Grundy
  • @Grundy perhaps it was not important to increase the array element by 10, but rather to convert it on the fly, maybe +10, and maybe another action. - Jean-Claude

1 answer 1

How the call works.
In this case, the call applies to reduce , and you want to add an argument to the callback , right? Will not work.
call only changes this to reduce (and this does not need to be done, otherwise it will be an error) and can add currying to reduce , not to the callback .
Process the array with logic and then reduce it to one number.

 let array = [1, 55, 399, 100], newArray = array.map(e => e += 10).reduce((a, e) => {a += e; return a;}); document.writeln('Before: ' + array + '<br />'); document.writeln('After: &nbsp;&nbsp;' + newArray); 

  • but this does not need to do, otherwise the error will be ? why there will be an error? - Grundy
  • @Grundy Used Strength, i.e. this inside. jsfiddle.net/pqLj23eb/1 The error is not in JS, but in the expected behavior. Although it is also possible to knock out an interpreter error if this is incorrect. - user207618
  • In the example of the link, the call itself changes its this and there is no error. Well, call currying cannot add, because it returns no function - Grundy
  • @Grundy There is no mistake you say ... i.stack.imgur.com/sCjlo.png About currying agree, there is a substitution only in place, Function.prototype.bind needed for full currying. - user207618
  • Well, most operations with null will return an error. besides, in the other case it's ok :) - Grundy