使用listobj.splice(i,1)后为什么JSON.stringify不起作用?

时间:2014-08-25 09:23:14

标签: json angularjs

我有一个JSON对象,其中包含员工的信息,多个元素可能具有相同的employee_id。我需要仅根据唯一的employee_id

清理此JSON列表
$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
  }
];

在此我使用:

$ scope.removeDupFromList($ scope.employeeRewardData);

//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;
        }

但是当我进入这个循环时,突然JSON.stringify停止工作。我无法理解Splice这种奇怪的行为。请帮帮我吗?

2 个答案:

答案 0 :(得分:2)

你真的应该使用一个库来做这些标准的东西。像Lo-Dash这样的库为各种各样的东西提供了功能,包括消除列表中的重复项。它们也经过充分测试,通常具有良好的性能。

这就是你的代码使用Lo-Dash的样子。

var uniqueEmployeeRewardData = _.uniq($scope.employeeRewardData, 'employee_id')

答案 1 :(得分:1)

使用Underscore.JS你也可以这样做:(不要重新发明轮子

$scope.employeeRewardDataNew =  _.uniq($scope.employeeRewardData, 
                                       function(item){
                                           return item.employee_id;
                                       });

演示 Fiddle


参考:

  

uniq_.uniq(array,[isSorted],[iterator])别名:唯一   生成数组的无副本版本,使用===来测试对象相等性。如果您事先知道数组已排序,则为isSorted传递true将运行更快的算法。如果要根据转换计算唯一项,请传递迭代器函数。

     

_.uniq([1,2,1,3,1,4]);   =&GT; [1,2,3,4]

您可以从http://underscorejs.org/underscore.js

获取来源