I am trying to learn how to use the abbreviation here, and this will only be logged. b,c,d
b,c,d
['a', 'b', 'c', 'd'].reduce(function(acc, cur){ console.log(cur); return cur; })
I do not understand what a battery is. I do not know how this works even after reading the documentation.
Currentdoes not appear as Currentbecause it is 'a'skipped.
Current
'a'
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
Edit: here is the best set of test cases:
reduce , , () .
reduce
, [a, b, c, d].reduce(f), :
[a, b, c, d].reduce(f)
f(a, b) -> f(f(a, b), c) -> f(f(f(a, b), c), d)
, a .
a
( index array, f)
index
array
f
" ", , , - . , , , acc cur "a" "b"
acc
cur
"a"
"b"
reduceRight :
reduceRight
f(c, d) -> f(b, f(c, d)) -> f(a, f(b, f(c, d)))
initialValue, , API Array#reduce.
initialValue
Array#reduce
InitialValue[] . , . - .
InitialValue
[] . , . - .
['a', 'b', 'c', 'd'].reduce(function(acc, cur){ console.log(cur); return cur; }, undefined); // ^^^^^^^^^
, , , :
let products = [ { label: 'Product 1', price: 5 }, { label: 'Product 2', price: 15 }, { label: 'Product 3', price: 20 }, ];
:
let totalPrice = products.reduce((acc, product) => { return acc + product.price; }, 0); // 0 - initial value console.log(totalPrice);
a, , .
- [0], - [1].
- f ([0], [1]), - [2] ..
docs,
reduce() ( ), .
,
reduce acc a,b,c cur b,c,d
a,b,c
, a,b b,c, c,d
a,b
b,c
c,d
['a', 'b', 'c', 'd'].reduce(function(acc, cur){ console.log(acc); return acc+cur; })
ab abc abcd
array.reduce() . () . Accumulator .
Source: https://habr.com/ru/post/1675075/More articles:ElasticSearch - add to integer array - arraysHow to call a function from the constructor of the JS class - javascriptWhy does https.Agent throw a parsing error when proxing through node-http-proxy? - javascriptHow to compile Swift from a command line for distribution - swiftAngular 2 Testing a component with observable, no bullying - angularПочему тензорный поток использует менее 20% GEFORCE 1080 ti (11 ГБ)? - tensorflowHow to pass a string to a post call using python queries - pythonMigrating images in a tensor stream, changing a simple softmax layer to multilayer CNN - pythonConvert Leaflet LatLng for overlaying x, y pixel images - javascripthttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1675080/swift-3-function-in-protocol-that-return-object-of-class-that-conforms-to-a-protocol&usg=ALkJrhiR6W0wvlSax7ayCM7QTVBlAPtTaQAll Articles