Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

So in my application I have small blocks of info called suntis. Each sunti can have .tags, .author, .ancestor, .short_id, and some other attributes.

Say I have a sunti with the following

sunti_one = {
    content: "The nature of mankind is deep-down inseparable from the nature of the cosmos",
    tags: "philosophy, cosmos, nature, man",
    ancestor: "none",
    short_id: "H18EIQ5p"
}

Then I have a sunti with the following, where the ancestor field points to the short_id of the above sunti:

sunti_two = {
    content: "It's true that man and the cosmos are entertwined, but man is clearly embedded in the cosmos.  Yet, does that mean necessarily that the cosmos is embedded in man, too?"
    tags: "cosmos, entertwined, man, question"
    ancestor: "H18EIQ5p", << right here
    short_id: BkwV9q2a  
}

I want to check incoming suntis to see if they have an ancestor_id that exists already in my stash/cache of suntis-seen-so-far.

Now I have something like

socket.on('load_sunti_from_cache', function(a_sunti_over_the_wire) {

        angular.forEach($scope.suntis, function(single_sunti_in_stash) {

            //check stash'd sunti short_ids against incoming ancestor_ids
            //console.log(a_sunti_over_the_wire.ancestor + " \\\ over wire")
            //console.log(single_sunti_in_stash.short_id + " /// sunti stash")

            if (a_sunti_over_the_wire.ancestor == single_sunti_in_stash.short_id) {
                console.log('Matching ancestor & short_id: ' + single_sunti_in_stash.short_id)
                $scope.suntis. ...? (what do I put here so that the over_the_wire sunti gets appended as a descendent of the matching ancestor sunti?)
            }
        })

I want the result to be:

{
        content: "The nature of mankind is deep-down inseparable from the nature of the cosmos",
        tags: "philosophy, cosmos, nature, man",
        ancestor: "none",
        short_id: "H18EIQ5p",
        descendents: {
                      {
                      content: "It's true that man and the cosmos are entertwined, but man is clearly embedded in the cosmos.  Yet, does that mean necessarily that the cosmos is embedded in man, too?"
                      tags: "cosmos, entertwined, man, question"
                      ancestor: "H18EIQ5p"
                      short_id: BkwV9q2a  
                      }
                     }
 }
share|improve this question
    
what is the specific problem or question? – charlietfl 21 hours ago
    
Well I am solving this using Two (json) arrays now, $scope.suntis and $scope.sunti_descendents, but I originally wanted to have nested JSON. I get a new sunti; check if it has an ancestor already in my sunti array, and if it does, add it as a nested json object. My question is how to add nested json via angular. – sova 21 hours ago
1  
descendants needs to be array...push into array as needed. would be a lot simpler if you had a parentId in the child elements – charlietfl 21 hours ago
    
@charlietfl Thank you charlie, if you could post an answer saying the same I would gladly accept it. I had to check if there was a descendents property, if not then create an empty descendents array $scope.suntis[sunti_stash_index].descendents = [] and push into it as you said. – sova 13 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.