reducers code example
Example 1: reduce
const array1 = [1, 2, 3, 4];
const reducer = (accumulator, currentValue) => accumulator + currentValue;
console.log(array1.reduce(reducer));
console.log(array1.reduce(reducer, 5));
Example 2: react/redux reducers sintaxe
const reduxSinaxe = (state = [], action) => {
switch (action.type) {
case "@smthg/ACTION":
const { anyConst } = action;
return [...state, anyConst];
default:
return state;
}
};
export default reduxSinaxe;
Example 3: Reducer
reduce((accumulator, currentValue) => { ... } )
reduce((accumulator, currentValue, index) => { ... } )
reduce((accumulator, currentValue, index, array) => { ... } )
reduce((accumulator, currentValue, index, array) => { ... }, initialValue)
reduce(callbackFn)
reduce(callbackFn, initialValue)
reduce(function callbackFn(accumulator, currentValue) { ... })
reduce(function callbackFn(accumulator, currentValue, index) { ... })
reduce(function callbackFn(accumulator, currentValue, index, array){ ... })
reduce(function callbackFn(accumulator, currentValue, index, array) { ... }, initialValue)
Example 4: reduce
var sum = _.reduce([1, 2, 3], function(memo, num){ return memo + num; }, 0);
=> 6