【问题标题】:Why JSON.stringify is not working after I use listobj.splice(i,1)?为什么我使用 listobj.splice(i,1) 后 JSON.stringify 不起作用?
【发布时间】: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 和我能回答的唯一原因是因为您的要求非常标准,以至于您不应该首先为它编写算法。 :)
  • 哦,感谢您指出我做错的地方。从现在开始,我一定会牢记这些事情。

标签: json angularjs


【解决方案1】:

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

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

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

【讨论】:

  • 我替换了对函数 $scope.removeDupFromList($scope.employeeRewardData); 的调用with _.uniq($scope.employeeRewardData, 'employee_id');但它不会删除重复的元素。我的意思不是元素是重复的,而是多个元素具有相同的 employee_id ,无论我保留哪一个,我都需要唯一的列表。关于为什么这可能不起作用的任何想法?
  • Maxim Shoustin 提供的调用对我有用。但同时,感谢您告诉我使用库。感谢您的帮助。
  • 它应该可以工作。看看这个fiddle。马克西姆的答案也应该适用于 Lo-Dash。下划线和 Lo-Dash 非常相似。有关差异,请参阅here
  • 哦,对不起,这是我的错。我虽然输入本身会被清除,但忘记了我们需要将输出放在单独的变量中(或用新结果替换输入)。非常感谢雨果。
  • 啊,是的,我应该提到这一点。我会修改我的答案,这样以后的读者就不会犯同样的错误了。
【解决方案2】:

使用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]); => [1, 2, 3, 4]

您可以从http://underscorejs.org/underscore.js获取资源

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 2013-07-15
    • 2014-11-10
    • 2012-03-19
    • 2012-08-25
    相关资源
    最近更新 更多