How To Use .map() In Angular

In this blog, you will learn how to use .map() in Angular.

In this blog, your going to see how to use .map() functions in Angular. I will explain to you how it works with a simple example.
 
You have received an array containing multiple objects, each one representing an employee detail. You need to retrieve the id of each employee in an array object.
 
  1. // What you have  
  2.   
  3. var employees = [  
  4.  { id: 11, name: 'Ravishankar' },  
  5.  { id: 12, name: 'Muthu' },  
  6.  { id: 13, name: 'Mani' },  
  7.  { id: 14, name: 'Ajith' }  
  8. ];  
  9.   
  10. // What you need  
  11. [11, 12, 13, 14]  

You can achieve this using .map()like below.

  1. var employeeIds = employees.map(employee => employee.id);  
The callback runs for each value in an array and returns new value in the resulting array.  The resulting array length will be the same as the original array
 
That’s it. I hope you have learned how to use .map() in Angular.