UNPKG

can-define

Version:

Create observable objects with JS dot operator compatibility

42 lines (31 loc) 1.73 kB
@function can-define/list/list.prototype.reduceRight reduceRight @parent can-define/list/list.prototype @description Map the values in this list to a single value from right to left @signature `list.reduceRight(callback, initialValue, [, thisArg])` Loops through the values of the list in reverse order, 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 `reduceRight`. ```js import {DefineList} from "can"; const todos = new DefineList([ {name: "dishes", complete: false}, {name: "lawn", complete: true} ]); const todosAsOneObject = todos.reduceRight((todos, todo) => { todos.push([todo.name, todo.complete]); return todos; }, []); console.log(todosAsOneObject); //-> [["lawn", true], ["dishes", false]] ``` @codepen @param {function(item, index, list)} callback 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 `DefineList` the elements are coming from. The return value of `callback` is passed to the next iteration as the first argument, and returned from `reduceRight` if the last iteration. @param {*} [initialValue] The initial value to use as `current` in the first iteration @param {Object} [thisArg] The object to use as `this` inside the callback. @return {*} The result of the final call of `callback` on the list.