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