如何在现有Array中查找重复值并在angularjs

时间:2016-09-15 09:55:01

标签: angularjs arrays

/ 我的json数据。 /

$ scope.fund = [         {             id:1,             基金名称:' HDFC中期机会-G',             日期:' 12/05 / 2016',             amountunits:' 11120',             价格:' 200'         },         {             id:2,             基金名称:' HDFC中期机会-D',             日期:' 12/05 / 2016',             amountunits:' 11120',             价格:' 200'         },         {             id:1,             基金名称:' HDFC Medium Ter',             日期:' 12/05 / 2016',             amountunits:' 11120',             价格:' 200'         },           {             id:1,             基金名称:' HDFC Medium',             日期:' 12/05 / 2016',             amountunits:' 11120',             价格:' 200'         },         {             id:3,             基金名称:' HDFC中期-G',             日期:' 12/05 / 2016',             amountunits:' 11120',             价格:' 200'         },     ]

/ 我想这样输出。 /

$ scope.newArray = [         {             id:1,             基金名称:' HDFC中期机会-G',             日期:' 12/05 / 2016',             amountunits:' 11120',             价格:' 200'         },         {             id:1,             基金名称:' HDFC Medium Ter',             日期:' 12/05 / 2016',             amountunits:' 11120',             价格:' 200'         },           {             id:1,             基金名称:' HDFC Medium',             日期:' 12/05 / 2016',             amountunits:' 11120',             价格:' 200'         },

]

2 个答案:

答案 0 :(得分:0)

您可以采用旧方式:循环遍历数组,如果临时数组中不存在ID,请将其放在那里,否则将其放入重复的数组中。

答案 1 :(得分:0)

试试这样..

    var arr = [];
    var duparr = [];
    angular.forEach($scope.fund, function(value, key){
       if(!arr.hasOwnProperty(value['id'])){
           arr.push(value);
       }else{
           duparr.push(value);
       }
    });
    angular.forEach(arr, function(value, key){
           if(duparr[0]['id'] == value['id']){
                 duparr.push(value);
            }
    });

    $scope.newArray = duparr;
    console.log($scope.newArray);