I try generate String from an array of JavaScript objects using the .map()
method, with below code:
var array1 = [{
"DepartmentId": 155,
"DepartmentName": "Animation",
"Selected": true
},
{
"DepartmentId": 156,
"DepartmentName": "Software Development",
"Selected": false
},
{
"DepartmentId": 161,
"DepartmentName": "Testing",
"Selected": false
},
{
"DepartmentId": 160,
"DepartmentName": "Account",
"Selected": true
}
];
// pass a function to map
const map1 = array1.map(function(e) {
if (e.Selected == true) return e.DepartmentId;
}).join(',');
console.log(map1);
My expected output is: 155,160
but it gives me
Actual output is: 155,,,160