Requirement: Let us assume we called REST Web Service, which returned us multiple duplicate departmentId and departmentName, and I want to display only the unique combinations of departmentId and departmentName. Hence for such a case, we can pass the entire data into the JavaScript function and then filter out the duplicate data and return only the unique data.
Solution: For the solution of the above requirement we will use the below JavaScript code.
define([], function() {
'use strict';
var PageModule = function PageModule() {};
PageModule.prototype.uniqueDepartmentDetails = function(result) {
var uniqueResult = [];
var final = [];
if (result && result.length > 0) {
for (var i = 0; i < result.length; i++) {
if (!uniqueResult[result[i].departmentId]) {
final.push({
departmentId: result[i].departmentId,
departmentName: result[i].departmentName,
});
uniqueResult[result[i].departmentId] = 1;
}
}
}
return final;
};
return PageModule;
});Hence, the solution to our requirement.
If you like the post please comment, share, and do join me on Facebook. Please subscribe to my YouTube Channel for video tutorials.
Thanks & Regards,
Susanto Paul
379 total views, 1 views today