Arrays in TS
Course: Typescript - chapter 8
If you want to access arrays in typescript, it’s as simple as adding a [] annotation next to the type
Example 1
Section titled “Example 1”Given an array of positive integers as input, return the maximum value in the array
Solution
function maxValue(arr: number[]) { let max = 0; for (let i = 0; i < arr.length; i++) { if (arr[i] > max) { max = arr[i] } } return max;}
console.log(maxValue([1, 2, 3]));Example 2
Section titled “Example 2”Given a list of users, filter out the users that are legal (greater than 18 years of age)
interface User { firstName: string; lastName: string; age: number;}Solution
interface User { firstName: string; lastName: string; age: number;}
function filteredUsers(users: User[]) { return users.filter(x => x.age >= 18);}
console.log(filteredUsers([{ firstName: "harkirat", lastName: "Singh", age: 21}, { firstName: "Raman", lastName: "Singh", age: 16}, ]));