reduce
Map the values in this list to a single value
list.reduce(callback, initialValue, [, thisArg])
Loops through the values of the list, calling callback for each one until the list
ends. The return value of callback is passed to the next iteration as the first argument,
and finally returned by reduce.
import {DefineList} from "can";
const todos = new DefineList([
{name: "dishes", complete: false},
{name: "lawn", complete: true}
]);
const todosAsOneObject = todos.reduce((todos, todo) => {
todos[todo.name] = todo.complete;
return todos;
}, {});
console.log(todosAsOneObject); //-> { dishes: false, lawn: true }
Parameters
- callback
{function(item, index, list)}:A function to call with each element of the DefineList. The four parameters that callback gets passed are:
- current (*) - the current aggregate value of reducing over the list -- the initial value if the first iteration
- item (*) - the element at index.
- index (Integer) - the index of the current element of the list.
- list (DefineList) - the
DefineListthe elements are coming from.
The return value of
callbackis passed to the next iteration as the first argument, and returned fromreduceif the last iteration. - initialValue
{*}:The initial value to use as
currentin the first iteration - thisArg
{Object}:The object to use as
thisinside the callback.
Returns
{*}:
The result of the final call of callback on the list.