Filter data from an array in angularjs

0

I have the following arrangement of objects:

$scope.name = {
  "cliente":[
    {"id":1, "dato":"des"},{"id":2, "dato":"eeeee"}
   ],
  "detalle":[{"id":1, "dato":"oooo"},{"id":2, "dato":"ccccc"},{"id":2, "dato":"xxxxxxxxx"},{"id":2, "dato":"zzzzzzz"}]

}

which is traversed to assign to a new array all the data corresponding to the id of the client object. This is my controller

function MyCtrl($scope) {
    $scope.arreglo = [];
    $scope.name = {
  "cliente":[
    {"id":1, "dato":"des"},{"id":2, "dato":"eeeee"}
   ],
  "detalle":[{"id":1, "dato":"oooo"},{"id":2, "dato":"ccccc"},{"id":2, "dato":"xxxxxxxxx"},{"id":2, "dato":"zzzzzzz"}]

}
for(var i=0; i<$scope.name.cliente.length;i++){
  for(var j=0; j<$scope.name.detalle.length;j++){
    if($scope.name.cliente[i].id == $scope.name.detalle[j].id){
      $scope.arreglo.push({"cliente":$scope.name.cliente[i].id, "detalle":$scope.name.detalle[j]})
    }
  }
}
    console.log(JSON.stringify($scope.arreglo))
}

When printing $ scope.arrange the result is as follows:

[{"cliente":1,"detalle":{"id":1,"dato":"oooo"}},{"cliente":2,"detalle":{"id":2,"dato":"ccccc"}},{"cliente":2,"detalle":{"id":2,"dato":"xxxxxxxxx"}},{"cliente":2,"detalle":{"id":2,"dato":"zzzzzzz"}}]

and what I want is for it to be presented in this way:

[{"cliente":1,"detalle":{"id":1,"dato":"oooo"}},
{"cliente":2,"detalle":{"id":2,"dato":"ccccc", "dato":"xxxxxxxxx","dato":"zzzzzzz"}}]

What I really want is that the customer id and its associated data are presented once only

    
asked by Dimoreno 01.02.2017 в 16:42
source

2 answers

0

var names = {"cliente":[
    {"id":1, "dato":"des"},{"id":2, "dato":"eeeee"}
   ],
    "detalle": [{id:1, "dato":"oooo"},{id:2, "dato":"ccccc"},{id:2, "dato":"xxxxxxxxx"},{id:2, "dato":"zzzzzzz"}]
 }


var result= names.detalle.reduce((acc, item) => {
  let { id, dato } = item;
  id="cliente "+id;
  acc[id] = acc[id] || [];
  dato="dato "+dato;
  acc[id].push(dato);
  return acc;
}, {});

console.log(result);
    
answered by 01.02.2017 / 19:35
source
0

Yes, I think it's more or less what you want, I think it's not necessary to place the client-id, in the detail because you're already living in the client for the output object. You should also be clear about the difference between defining the detail as an array [] or as attributes in an object {}, when you said "I want it to be that way", it would be as if it were a dynamic object, it could be, but it seems that the regular structure it became more natural in this case, unless you are sure why you prefer it to be an object with N attributes and have the reason to prefer it before an arrangement

for(var i=0; i<$scope.name.cliente.length;i++){
  var cliente = {"cliente":$scope.name.cliente[i].id,"detalle":[]};
  for(var j=0; j<$scope.name.detalle.length;j++){
    if($scope.name.cliente[i].id == $scope.name.detalle[j].id){
       cliente.detalle.push($scope.name.detalle[j]);
    }
  }
  $scope.arreglo.push(cliente);
}
    
answered by 01.02.2017 в 19:07