2

Trying to copy a part of a json object into another json object (thats a filter), into a for loop, under a conditional statement, it doesn't work.

This work but a plain to write an array:

$scope.candidats=[];
for (i=0;i<omegaCandidats.length;i++){
    if (omegaCandidats[i].dateRdv==date){ 
        $scope.candidats.push(
            {
             "id"       :omegaCandidats[i].id,
             "prenom"   :omegaCandidats[i].prenom,
             "nom"      :omegaCandidats[i].nom,
             "heure"    :omegaCandidats[i].heure,
             "dateRdv"  :omegaCandidats[i].date
            }
        )
    };
};

This doesn't work, and that's what i want to do. Its logical and should work:

$scope.candidats=[];
for (i=0;i<omegaCandidats.length;i++){
    if (omegaCandidats[i].dateRdv==date){ 
        $scope.candidats[i] = omegaCandidats[i];
    };
};

This one work but only get one value of the for loop its useless:

$scope.candidats=[];
for (i=0;i<omegaCandidats.length;i++){
    if (omegaCandidats[i].dateRdv==date){ 
        $scope.candidats[0] = omegaCandidats[i];
    };
};
1
  • Do not forget to accept answers (I say this also for your old questions) Commented Apr 24, 2015 at 15:08

3 Answers 3

3

what about using a filter:

$scope.candidats = omegaCandidats.filter(function(candidat){ 
    return candidat.dateRdv == date;
});
Sign up to request clarification or add additional context in comments.

Comments

2

You can use filter array method, try this:

$scope.candidats = omegaCandidats.filter(function(item) {
    return item.dateRdv==date;
});

2 Comments

:) beat you by 8 seconds
Thank you a lot, you are impressive !
0

I think this should work :

$scope.candidats=[];
    for (i=0;i<omegaCandidats.length;i++){

        if (omegaCandidats[i].dateRdv==date){ 

            //$scope.candidats.push(omegaCandidats[i]);

            $scope.candidats.push(angular.copy(omegaCandidats[i]));
            //copy will create a new reference for your object.
        };
    };

The code you had is not logical to me :

$scope.candidats=[];
    for (i=0;i<omegaCandidats.length;i++){
        if (omegaCandidats[i].dateRdv==date){ 
            $scope.candidats[i] = omegaCandidats[i]; // This can't work because $scope.candidats[i] is not defined.
            // You would also have an inconsistent array
        };
    };

6 Comments

Woow thanks a lot let's try this and the others solutions too, u're awesome tx a lot !
I've edited my answer. I'm not sure if you need to copy or not your values.
Yees ! It works u're really great thank a lot, it will save me a lot of times, i'll try the filters too it's more logical. Angular is fantastic.
yup, for this particular problem, a filter is more suitable
True about the inconsistent array, I agree ! Thank you again a lot. it will help for my project nicolash.org/angular/app7.html
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.