如何在angularJS中基于id过滤数组

时间:2017-07-04 09:51:41

标签: angularjs

我有一个id的多个数据,我希望像这样过滤我的数据

$scope.mpArray =[
{ Id: 1, Name: Madhu, Address: Upal },
{ Id: 1, Name: Chandu, Address: Upal },
{ Id: 2, Name: Srinu, Address: Kphb },
{ Id: 2, Name: Vijay, Address: kphb },
{ Id: 3, Name: Ajay, Address: Banglore },
{ Id: 3, Name: Narsi, Address: Banglore },
{ Id: 3, Name: Peter, Address: Banglore },
];

我想像这样过滤我的数组

var FilterArray = [
{ Id: 1,Madhu, Chandu},
{ Id: 2, Srinu, Vijay},
{ Id: 3, Ajay, Narsi, Peter},
];

3 个答案:

答案 0 :(得分:0)

首先,您需要将FilterArray更改为

[
  {
    "Id": 1,
    "Name": [
      "Madhu",
      "Chandu"
    ]
  },
  {
    "Id": 2,
    "Name": [
      "Srinu",
      "Vijay"
    ]
  },
  {
    "Id": 3,
    "Name": [
      "Ajay",
      "Narsi",
      "Peter"
    ]
  }
]

请注意,名称是array。您问题的FilterArray

var FilterArray = [
{ Id: 1,Madhu, Chandu},
{ Id: 2, Srinu, Vijay},
{ Id: 3, Ajay, Narsi, Peter},
];

不要在数组中包含有效的JSON对象,因此您需要将结构更改为在Name的JSON对象中添加新键FilterArray的结构,就像上面的第一个结构一样。然后下面的代码很有用。

$(document).ready(function(){
var myArray =[
{ Id: 1, Name: "Madhu", Address: "Upal" },
{ Id: 1, Name: "Chandu", Address: "Upal" },
{ Id: 2, Name: "Srinu", Address: "Kphb" },
{ Id: 2, Name: "Vijay", Address: "kphb" },
{ Id: 3, Name: "Ajay", Address: "Banglore" },
{ Id: 3, Name: "Narsi", Address: "Banglore" },
{ Id: 3, Name: "Peter", Address: "Banglore" },
];
var FilterArray = [];
var matched;
  for(var i=0;i<myArray.length; i++){
     matched = false;
     var myArrayId = myArray[i].Id;
       for(var j=0; j<FilterArray.length; j++){
         var FilterArrayId = FilterArray[j].Id;
          if(myArrayId === FilterArrayId){
              matched = true;
              FilterArray[j].Name.push(myArray[i].Name);
              // no need to loop further
              break;
          }
       }
       if(!matched){
           var obj = {
              'Id' : myArrayId,
              'Name' : [myArray[i].Name],
           }
           FilterArray.push(obj);
       }
  }
 console.log(FilterArray);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

答案 1 :(得分:0)

试试这个

var mpArray =[
{ Id: 1, Name: 'Madhu', Address: 'Upal' },
{ Id: 1, Name: 'Chandu', Address: 'Upal' },
{ Id: 2, Name: 'Srinu', Address: 'Kphb' },
{ Id: 2, Name: 'Vijay', Address: 'kphb' },
{ Id: 3, Name: 'Ajay', Address: 'Banglore' },
{ Id: 3, Name: 'Narsi', Address: 'Banglore' },
{ Id: 3, Name: 'Peter', Address: 'Banglore' },
];

var filterObject = {};

mpArray.forEach(function (item) {
    if (!filterObject[item.Id]) {
        filterObject[item.Id] = [];
    }
    
    filterObject[item.Id].push(item.Name);
});

console.log(filterObject);

答案 2 :(得分:0)

$scope.mpArray =[
                    { Id: 1, Name: 'Madhu', Address: 'Upal' },
                    { Id: 1, Name: 'Chandu', Address: 'Upal' },
                    { Id: 2, Name: 'Srinu', Address: 'Kphb' },
                    { Id: 2, Name: 'Vijay', Address: 'kphb' },
                    { Id: 3, Name: 'Ajay', Address: 'Banglore' },
                    { Id: 3, Name: 'Narsi', Address: 'Banglore' },
                    { Id: 3, Name: 'Peter', Address: 'Banglore' },
                    ];
                    var FilterArray = [];
                    var FilteredArrayIds=[];
                    $scope.mpArray.forEach(
                                            function(detailObj) {
                                                if(FilteredArrayIds.indexOf(detailObj.Id)==-1)
                                                 return FilteredArrayIds.push(detailObj.Id);
                                        });
                    for(var i=0; i<FilteredArrayIds.length;i++)
                    {
                          var result = $scope.mpArray.filter(function( obj ) {
                              return obj.Id == FilteredArrayIds[i];
                          });
                          var rsltNames = result.map(function(obj){
                            return obj.Name;
                          })
                          var filteredObj ={
                            id:FilteredArrayIds[i]+',' +rsltNames.join()
                          }
                          FilterArray.push(filteredObj);
                    }
                    console.log(filteredObj)