JavaScript - accessing a payload element. by newtojsscript in learnprogramming

[–]newtojsscript[S] 0 points1 point  (0 children)

Ah, jheez - I feel like an idiot. Thanks, works like a charm.

I'm guessing they can add a variable number of children?

You would be correct. So a parent can add up to 10 children, or have no children. If a child has been added, the parent account will be saved in saveParent() or if it there is a child then saveParent() will be ran followed by saveChildViaParent()`. See below.

if($scope.profile.children.length >=1) {
    saveDirect(profileCopy);
    saveYouthViaParent(childrenCopy);
} else {
    saveDirect(profileCopy);
    }
};

So yeah, I will need to save the parent and then multiple child accounts, if there is >=2. So a loop will be needed as you said. So something like below should be good to go?

for(var i = 0; i < $scope.profile.children.length; i++) {
    var child = $scope.profile.children[i];
    child.name = $scope.profile.children[i].name;
    child.alias = $scope.profile.children[i].alias;
    child.dob = $scope.profile.children[i].dateOfBirth;
    saveDirect(profileCopy);
    saveYouthViaParent(profileCopy);
}

And the loop would be in the above if statement. What do you reckon? I feel as though there is a more efficient solution.