0

I have JSON string of Nested Arrays with unequal length elements as shown below , I want to display it in a page using Javascript or Angular.js.

{ [
   'id':1,
   'value':'tes1'
   'nextLevel':['id':12,
     'value':'tes12'
     'nextLevel':['id':13,
                  'value':'tes13',
                   nextLevel:null ],
     ]

   ],

['id':2,
   'value':'tes2'
   'nextLevel':null ]
]

here nextLevel can be null or it can have another array nested, nested array in turn will have another nested array.

I want to display all array elements into a list, with Parent list element have link to child list Elements, until no child element found.

can somebody post the steps on how to perform this task? As I am able to find example for equal length array, but no example can be found for this nested case.

1
  • What attempts have you made? Where are you running into problems? Commented Sep 15, 2014 at 19:48

2 Answers 2

1

I think this would need what is considered a recursive function. Recursion (JavaScript)

Each time through the method, it checks to see if the next level is null, and if it isn't it "recursively" calls itself, passing the next level.

http://jsfiddle.net/k8Lratja/

var ary = {
    'id': 1,
        'value': 'tes1',
        'nextLevel': {
        'id': 12,
            'value': 'tes12',
            'nextLevel': {
            'id': 13,
                'value': 'tes13',
                'nextLevel': null
        }
    }
};

function objectDisplay(objectIn) {
    document.write(objectIn.id + ' ' + objectIn.value + "<br />");
    if (objectIn.nextLevel !== null) {
        objectDisplay(objectIn.nextLevel);
    }
}

objectDisplay(ary);

Sign up to request clarification or add additional context in comments.

Comments

1

Angular example

<div class="ctrl" ng-app="app" ng-controller="ctrl" >
     <div ng-repeat="idLevel in data">
        <div>
            {{idLevel.id}} = {{idLevel.value}}
            <div ng-repeat="nextLevel1 in idLevel.nextLevel" style="margin-left: 20px;">
                {{nextLevel1.id}}={{nextLevel1.value}}
                <div ng-repeat="nextLevel2 in nextLevel1.nextLevel" style="margin-left: 20px;">
                    {{nextLevel2.id}}={{nextLevel2.value}}
                    <div ng-repeat="nextLevel3Item in nextLevel2.nextLevel" style="margin-left: 20px;">
                        {{nextLevel3Item.id}}={{nextLevel3Item.value}}
                    </div>
                </div>
            </div>
        </div>
    </div> 
</div>

3 Comments

Going by this sentence in the question "I want to display all array elements into a list, with Parent list element have link to child list Elements, until no child element found." it makes it sound like the list presented in the question is not always going to look exactly like that. It will have varying amounts of nextLevel in each level. I don't think your solution will handle anything other than the exact object in the question though.
Since the problem description wasn't exactly precise I assumed that the case here were 'null' nodes.
Hi, thanks for your answer, is there any way to ng-repeat arbitrary level, without using <div>

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.