【发布时间】:2014-08-25 09:23:13
【问题描述】:
我有一个包含员工信息的 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 的这种奇怪行为。
【问题讨论】:
-
好的。有人否决了我的问题。先生。 Downvoter,请尽量理解我在这里发帖不是为了玩,我发帖是为了解决对你来说可能看起来很幼稚的问题,但对于像我们这样刚开始学习新事物的开发者来说仍然是“问题”。不要气馁。
-
由于我和 Maxim 将您导向库解决方案,而没有更正您的代码,我想指出一些令人不安的事情。 1) 迭代列表时不应修改列表(添加或删除项目)。您应该创建一个新列表。 2)为什么要对列表进行字符串化以在之后对其进行解析?这没有任何意义。 3)当您需要在集合中按值进行大量查找时,请考虑映射或集合而不是数组。
-
我猜投反对票是因为您没有解释“停止工作”是什么意思。你应该描述错误是什么,它发生在什么迭代中,等等。您没有向我们提供足够的信息来帮助您。使用jsfiddle.net 的演示也会很有用。 Maxim 和我能回答的唯一原因是因为您的要求非常标准,以至于您不应该首先为它编写算法。 :)
-
哦,感谢您指出我做错的地方。从现在开始,我一定会牢记这些事情。