I have the following array 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 that corresponds 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.array 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 appear like this:
[{"cliente":1,"detalle":{"id":1,"dato":"oooo"}},
{"cliente":2,"detalle":{"id":2,"dato":"ccccc", "dato":"xxxxxxxxx","dato":"zzzzzzz"}}]
In itself what I want is that the client id is presented only once along with its associated data
Yes, I think that is more or less what you want, I think it is not necessary to place the client-id in the detail since it is 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 like this", it would be as if it were a dynamic object, it could be, but it seems that the regular structure came more naturally in this case, unless you're sure why you prefer it to be an object with N attributes and you have reason to prefer it that way over an array