Created
July 26, 2019 14:34
-
-
Save dmitrykuznetsovdev/66c26898682ccb3e3b839e672255097c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const userCollection = [ | |
{ | |
name: 'Дмитрий', | |
age: 35 | |
}, | |
{ | |
name: 'Сергей', | |
age: 30 | |
} | |
] | |
function reduce<T, U>(collection: T[], callback: (result: U, item: T) => U): T | |
function reduce<T, U>(collection: T[], callback: (result: U, item: T) => U, initItem: U): U | |
function reduce<T, U>(collection: T[], callback: (result: U, item: T) => U, initItem?: U): U { | |
let result: any = initItem !== undefined ? initItem : collection[0]; | |
let i = initItem !== undefined ? 0 : 1; | |
const count = collection.length; | |
for(i; i < count; i++) { | |
result = callback(result, collection[i]); | |
} | |
return result; | |
} | |
const result = reduce(userCollection, (userResult, current) => { | |
userResult[current.name] = current.age; | |
return userResult; | |
}); | |
console.log(result, "sum"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment