I have a JSON object which contains information of employees ,more than one element of may have same employee_id. I need to clean this JSON list solely on the basis of unique employee_id
$scope.employeeRewardData = [ { "cin_number": 0, "function_id": 3118, "grade_id": 163, "role_id": 15858, "location_id": 151, "employee_designation": "NA", "bu_id": 90, "gen_id": "GPR01", "reward_id": 0, "employee_id": 644, "reward_category_id": 2, "sepserial_no": 0, "sepemployee_id": 0, "fname": "Smiles", "emp_type": 381, "status": "Active", "location_name": "Corporate", "level_id": 207, "lname": "Administrator", "reward_cust_id": 0, "reward_to_userid": 644, "corresponding_status_id": 0, "company_id": 7, "reward_by_userid": 644 }, { "cin_number": 0, "function_id": 3118, "grade_id": 163, "role_id": 15858, "location_id": 151, "employee_designation": "NA", "bu_id": 90, "gen_id": "GPR01", "reward_id": 0, "employee_id": 644, "reward_category_id": 3, "sepserial_no": 0, "sepemployee_id": 0, "fname": "Smiles", "emp_type": 381, "status": "Active", "location_name": "Corporate", "level_id": 207, "lname": "Administrator", "reward_cust_id": 0, "reward_to_userid": 644, "corresponding_status_id": 0, "company_id": 7, "reward_by_userid": 644 }, { "cin_number": 0, "function_id": 175, "grade_id": 147, "role_id": 20469, "location_id": 152, "employee_designation": "Chief Officer Client Studio", "bu_id": 90, "gen_id": "GPR00082", "reward_id": 0, "employee_id": 741, "reward_category_id": 1, "sepserial_no": 0, "sepemployee_id": 0, "fname": "Sheena", "emp_type": 381, "status": "Active", "location_name": "Bangalore", "level_id": 178, "lname": "Sharma", "reward_cust_id": 0, "reward_to_userid": 741, "corresponding_status_id": 0, "company_id": 7, "reward_by_userid": 644 }, { "cin_number": 0, "function_id": 190, "grade_id": 224, "role_id": 665, "location_id": 151, "employee_designation": "Senior Manager - Knowledge", "bu_id": 90, "gen_id": "GPR00002", "reward_id": 0, "employee_id": 657, "reward_category_id": 2, "sepserial_no": 0, "sepemployee_id": 0, "fname": "Aishwarya", "emp_type": 381, "status": "Active", "location_name": "Corporate", "level_id": 270, "lname": "Singh", "reward_cust_id": 0, "reward_to_userid": 657, "corresponding_status_id": 0, "company_id": 7, "reward_by_userid": 644 } ];
On this I use :
$scope.removeDupFromList($scope.employeeRewardData);
which is
//Function to remove more than one presence of Elelments in Array $scope.removeDupFromList = function(listObj) { //Function will return a value which will be true if list is completely sorted var isListCompletelySorted=true; //This jsonObj will basically be $scope.matchingAndSelectedEmployeesList if(listObj!=null && listObj!=undefined && listObj.length>0) { for(var i=0;i<listObj.length;i++) { if(listObj[i]!=null && listObj[i]!=undefined) { for(var j=i+1;j<listObj.length;j++) { if(listObj[i]["employee_id"]==listObj[j]["employee_id"]) { listObj.splice(j, 1); isListCompletelySorted=false; } } } } } alert(".........."+listObj.length+"..............."); if(!isListCompletelySorted) { $scope.removeDupFromList(JSON.parse(JSON.stringify(listObj))); } else return isListCompletelySorted; }
But when I go in this loop suddenly JSON.stringify stops working. I am unable to understand this strange behaviour of Splice.
Answer
With Underscore.JS you can do the same: (Don’t reinvent the wheel)
$scope.employeeRewardDataNew = _.uniq($scope.employeeRewardData, function(item){ return item.employee_id; });
Demo Fiddle
Reference:
uniq_.uniq(array, [isSorted], [iterator]) Alias: unique Produces a duplicate-free version of the array, using === to test object equality. If you know in advance that the array is sorted, passing true for isSorted will run a much faster algorithm. If you want to compute unique items based on a transformation, pass an iterator function.
_.uniq([1, 2, 1, 3, 1, 4]); => [1, 2, 3, 4]
You can get sources from http://underscorejs.org/underscore.js