Last active
January 18, 2018 04:04
-
-
Save dp-singh/34649607a75db11030ffcccb2270d2ba to your computer and use it in GitHub Desktop.
Typescript custom type
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
//JSON is way of communication between client and server | |
//custom type | |
type Task={ | |
id: number; | |
title: string; | |
detail: string; | |
createdAt: Date; | |
updateAt: Date; | |
} | |
//declaration of array | |
var arr: Task[]; | |
arr = [ | |
{ | |
id: 1234, | |
title: "Mr D", | |
detail: "Learning Array", | |
createdAt: new Date(), | |
updateAt: new Date() | |
}, | |
{ | |
id: 1235, | |
title: "Mr D", | |
detail: "Learning Array", | |
createdAt: new Date(), | |
updateAt: new Date() | |
}, | |
{ | |
id: 1236, | |
title: "Mr D", | |
detail: "Learning Array", | |
createdAt: new Date(), | |
updateAt: new Date() | |
} | |
] | |
/** | |
* array , id as an input | |
* return the corresponding Task object | |
*/ | |
function findTask(arr: Task[], id: number): Task| undefined { | |
//confusing | |
//return tasks.find(task => task.id == id) | |
for (var index = 0; index < arr.length; index++) { | |
if (arr[index].id == id) | |
return arr[index]; | |
} | |
return undefined; | |
} | |
console.log(findTask(arr,1236)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment