Hello, I’m KANNAN Muthu Manickam
Full Stack – Software – QA/Test Automation Engineer
|
Software Development Engineer in Test (SDET)

Typescript Array of Objects

Typescript Array of Objects

Array

let’s explore examples of .map(), .filter(), and .find() in TypeScript.

1. map()

The .map() method is used to iterate over an array and transform each element into something else, returning a new array with the transformed elements. Here’s an example using TypeScript:

If you want to use .map() to transform only certain values in an array based on a condition in TypeScript, you can achieve this by including an if statement or a ternary operator inside the callback function. Here’s an example:

In this example, the .map() method iterates over each element in the numbers array. If the number is even (determined by num % 2 === 0), it squares the number (num * num). Otherwise, it leaves the number unchanged. The resulting array, transformedNumbers, contains the transformed values based on the condition.

This approach allows you to selectively transform array elements based on specific conditions using .map() in TypeScript.

2. filter()

The .filter() method is used to iterate over an array and return a new array containing only the elements that satisfy a condition. Here’s an example:

3. find()

The .find() method is used to find the first element in an array that satisfies a provided testing function. It returns the value of the first element in the array that satisfies the condition, or undefined if no such element is found. Here’s an example:

These examples demonstrate the basic usage of .map(), .filter(), and .find() in TypeScript. These methods are commonly used in functional programming and can significantly simplify array manipulation tasks.

Arrays of objects

From the above example

In the real time projects many JSON data like API responses need to validate with different combinations, lets take a sample scenario from the above jsonData: CompanyDetails need to get the employee DOB based on the the projectId as key for API response validation.

In the above code first we are filtering the team based on projectId from the jsonObject then from the team array finding each teamMember DOB

Kannan

Full Stack – Software – QA/Test Automation Engineer | SDET

Welcome to my website blog, I value your presence and perspective. Your feedback, comments, and interactions fuel my passion for what I do. Together, let’s embark on a journey of exploration, discovery, and growth.


Search in the Website


Subscribe to Newsletter

Stay updated with my latest blogs and ideas by joining the newsletter.


Old blog posts