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);
callparameter is the context — in fact, for thereducefunction — it must be an array or an ArrayLike object. Therefore, it is not entirely clear what you are trying to do after all - Grundycallat all, it suffices toreturn acc+el+10- Grundy