如何在现有数组中找到重复值,并在angularjs中push new Array所有重复值?

时间:2021-10-17 13:10:07

/My json data./

/我的json有悖于/

$scope.fund = [ { id: 1, fundname: 'HDFC Medium Term Opportunities-G', date: '12/05/2016', amountunits: '11120', price: '200' }, { id: 2, fundname: 'HDFC Medium Term Opportunitie-D', date: '12/05/2016', amountunits: '11120', price: '200' }, { id: 1, fundname: 'HDFC Medium Ter', date: '12/05/2016', amountunits: '11120', price: '200' }, { id: 1, fundname: 'HDFC Medium ', date: '12/05/2016', amountunits: '11120', price: '200' }, { id: 3, fundname: 'HDFC Medium Term -G', date: '12/05/2016', amountunits: '11120', price: '200' }, ]

美元的范围。基金=[{ id:1、fundname:“HDFC中期Opportunities-G”,日期:12/05/2016,amountunits:“11120”,价格:“200”},{ id:2,fundname:“HDFC中期Opportunitie-D”,日期:12/05/2016,amountunits:“11120”,价格:“200”},{ id:1、fundname:HDFC介质后,日期:12/05/2016,amountunits:“11120”,价格:“200”},{ id:1、fundname:“HDFC的媒介”,日期:12/05/2016,amountunits:“11120”,价格:“200”},{ id:3,fundname:HDFC中期- g,日期:12/05/2016,amountunits:“11120”,价格:“200”

/I want to output like this./

/我想这样输出。

$scope.newArray = [ { id: 1, fundname: 'HDFC Medium Term Opportunities-G', date: '12/05/2016', amountunits: '11120', price: '200' }, { id: 1, fundname: 'HDFC Medium Ter', date: '12/05/2016', amountunits: '11120', price: '200' }, { id: 1, fundname: 'HDFC Medium ', date: '12/05/2016', amountunits: '11120', price: '200' },

美元的范围。newArray = [{id: 1, fundname: 'HDFC中期机会- g ',日期:'12/05/2016',amountunits: '11120',价格:'200'},{id: 1, fundname: 'HDFC Medium Ter',日期:'12/05/2016',amountunits: '11120',价格:'200'},{id: 1, fundname: 'HDFC Medium ',日期:'12/05/2016',amountunits: '11120',价格:'200'},

]

2 个解决方案

#1


0  

You can do it the old fashion way: loop through the array and if the ID does not exist in an temporary array, put it there, otherwise put it in the duplicated array.

你可以用旧的方式来做:循环遍历数组,如果ID不存在于一个临时数组中,将它放在那里,否则将它放入复制的数组中。

#2


0  

Try like this..

这样的尝试。

    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);

#1


0  

You can do it the old fashion way: loop through the array and if the ID does not exist in an temporary array, put it there, otherwise put it in the duplicated array.

你可以用旧的方式来做:循环遍历数组,如果ID不存在于一个临时数组中,将它放在那里,否则将它放入复制的数组中。

#2


0  

Try like this..

这样的尝试。

    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);