Code to segregate arrays using the MAP function of JavaScript

Rajnilari2015
Posted by Rajnilari2015 under JavaScript category on | Points: 40 | Views : 1195
In the below code, we will find an example that segregate arrays using the MAP function of JavaScript

Say we have the below array

var rnaTeam = 
[
{
"EmpId": 1,
"EmpName": "Niladri Biswas",
"Deptid": 101,
"Deptname": "IT"
}, {
"EmpId": 2,
"EmpName": "Arina Biswas",
"Deptid": 201,
"Deptname": "Finance"
}, {
"EmpId": 3,
"EmpName": "Rajlaxmi Biswas",
"Deptid": 301,
"Deptname": "Doctor"
}, {
"EmpId": 4,
"EmpName": "RNA Team",
"Deptid": 101,
"Deptname": "IT"
}
];


The objective is to get the records pertaining to Employee and Department from rnaTeam array such that there will be two different arrays one of Employee and another of Department. The below code does so

var employees = rnaTeam.map(function(item) {
return {
EmpId: item.EmpId,
EmpName: item.EmpName
}
});
var departments = rnaTeam.map(function(item) {
return {
Deptid: item.Deptid,
Deptname: item.Deptname
}
});

Comments or Responses

Login to post response