0

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Rishi
  • 1,646
  • 2
  • 15
  • 34
  • OK.Somebody down voted my Question.Mr. Downvoter ,please try to understand I don't post here to play ,I post to get solution of the problems which might seem naive to you but for developers like us who have just started to learn new things are still "problems". Don't discourage us. – Rishi Aug 25 '14 at 10:06
  • 1
    Since Maxim and I oriented you towards library solutions, without correcting your code, I would like to point you to some things that are disturbing about it. 1) You should not modify a list (add or remove items) while you are iterating over it. You should create a new list. 2) Why are stringifying the list to parse it right after? It doesn't make any sense. 3) When you need to do lots of lookups by value in a collection, consider maps or sets instead of arrays. – Hugo Wood Aug 25 '14 at 15:16
  • 1
    I guess the down voting comes from the fact that you do not explain what you mean by "stops working". You should describe what the error is, at what iteration it occurs, so on and so forth. You do not give us enough information to help you. A demo using http://jsfiddle.net would have been useful too. The only reason Maxim and I could answer is because your requirement is so standard that you should not write an algorithm for it in the first place. :) – Hugo Wood Aug 25 '14 at 15:20
  • oh,Thanks for pointing out where I was doing wrong.I will certainly keep these things in mind from now on. – Rishi Aug 26 '14 at 05:42

2 Answers2

2

You really should use a library for such standard things. Libraries like Lo-Dash provide functions for all sorts of stuff including eliminating duplicates in lists. They are also well-tested and usually have good performance.

This is what your code would look like using Lo-Dash.

var uniqueEmployeeRewardData = _.uniq($scope.employeeRewardData, 'employee_id')
Hugo Wood
  • 2,140
  • 15
  • 19
  • I replace the calling to function $scope.removeDupFromList($scope.employeeRewardData); with _.uniq($scope.employeeRewardData, 'employee_id'); But it's not removing duplicate elements. I mean it's not that elements are duplicate but more than one element have same employee_id ,not matter which one I keep I need unique list. Any thought on why this might not be working? – Rishi Aug 25 '14 at 09:39
  • the calling Maxim Shoustin provide worked for me. But at the same time ,thank you for telling me to use libraries.Appreciate your help. – Rishi Aug 25 '14 at 09:45
  • It should work. Look at this [fiddle](http://jsfiddle.net/rmmbz6ny/). Maxim's answer should also work with Lo-Dash. Underscore and Lo-Dash are very similar. See [here](http://stackoverflow.com/questions/13789618/differences-between-lodash-and-underscore) for differences. – Hugo Wood Aug 25 '14 at 09:54
  • Oh I am sorry,it was my fault. I though input will itself get purged ,forgot that we need to take the output in separate variable(or replace the input with new result). Many thanks Hugo. – Rishi Aug 25 '14 at 10:03
  • Ah yes, I should have mentioned that. I'll edit my answer so future readers will not make the same mistake. – Hugo Wood Aug 25 '14 at 10:07
  • @MaximShoustin I really like to show my appreciation as Upvotes ,but I have just 13 points so unable to do that. :( – Rishi Aug 25 '14 at 10:33
1

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

Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225