Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const map = <T, R = T>(items: T[], cb: (item: T) => R): R[] => {
- const mapped: R[] = [];
- for (let item of items) {
- mapped.push(cb(item));
- }
- return mapped;
- };
- const mapx = (items, cb) => {
- const mapped = [];
- for (let item of items) {
- mapped.push(cb(item));
- }
- return mapped;
- };
- console.log(map(['1', '2', '3'], str => parseInt(str))[0]);
- interface Person {
- age: number;
- name: string;
- }
- interface Human {
- years: number;
- }
- map<Human, Person>([{ years: 12 }], ({ years: age }) => ({ age, name: 'unknown' }));
- map([{ years: 12 }], ({ years: age }) => ({ age }));
Add Comment
Please, Sign In to add comment