Skip to content

Instantly share code, notes, and snippets.

@dmitrykuznetsovdev
Created July 26, 2019 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmitrykuznetsovdev/66c26898682ccb3e3b839e672255097c to your computer and use it in GitHub Desktop.
Save dmitrykuznetsovdev/66c26898682ccb3e3b839e672255097c to your computer and use it in GitHub Desktop.
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