Last active
January 10, 2020 13:33
-
-
Save frivolta/07e2d49d713c0394ddbc95c078e4b72d to your computer and use it in GitHub Desktop.
This file contains 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
type Book = { | |
title: string; | |
genre: string; | |
publicationYear: number; | |
}; | |
type Car = { | |
modelName: string; | |
type: string; | |
price: number; | |
}; | |
const listOfBooks: Book[] = [ | |
{title: 'Dragon Of The Titans', genre: 'fantasy', publicationYear: 1992}, | |
{title: 'Queen Of Spring', genre: 'drama', publicationYear: 2005}, | |
{title: 'Wolves Of The King', genre: 'fantasy', publicationYear: 1988} | |
]; | |
const listOfCars: Car[] = [ | |
{modelName: 'Yellow Car', type: 'coupe', price: 20000}, | |
{modelName: 'Blue Car', type: 'SUV', price: 45000}, | |
{modelName: 'Green Car', type: 'coupe', price: 18000} | |
]; | |
const filterArrayByValue = <T, P extends keyof T>( | |
items: T[], | |
propertyName: P, | |
valueToFilter: T[P] //Partial<T> | |
): T[] => { | |
return items.filter(item =>item[propertyName] === valueToFilter); | |
}; | |
console.log(filterArrayByValue(listOfBooks,'genre','fantasy')); | |
console.log(filterArrayByValue(listOfCars,'type','SUV')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment