1
 <html>  
    <body>  
    <script type="text/javascript">  
    var categoryGroups = [
        {
            Id: 1, Categories: 
             [
                { Id: 1 },
                { Id: 2 }, 
            ]

        },
        {
            Id: 2, Categories: [
                { Id: 100 },
                { Id: 200 },
            ]

        }
    ]

    var category, categoryGroup, found = false;
    for (i = 0; i < categoryGroups.length ; i++) {
        categoryGroup = categoryGroups[i];
        for (j = 0; j < categoryGroup.Categories.length; j++) {
            category = categoryGroup.Categories[j];
            if (category.Id === categoryGroup.Id) {
                found = true;

                break;
    window.document.write("category" );
            }
        }
        if (found) break;
    }

    </script>  
    </body>  
    </html>  

i have created nested array.from this nested array i want print object of an nested array.but unable to find object of an nested array. how to resolve this error?

5
  • which object like you to print? Commented Jul 1, 2016 at 6:55
  • i want to print any one object of this nested array Commented Jul 1, 2016 at 6:56
  • for example like { Id: 1 }? and what do you do with category.Id === categoryGroup.Id? Commented Jul 1, 2016 at 6:57
  • yes.like that only Commented Jul 1, 2016 at 6:58
  • category.Id === categoryGroup.Id with this i was finding child object Commented Jul 1, 2016 at 7:00

2 Answers 2

1

Move break; below the output, because break ends the iteration and nothing behind gets executed.

window.document.write("category");
break;

var categoryGroups = [{ Id: 1, Categories: [{ Id: 1 }, { Id: 2 }, ] }, { Id: 2, Categories: [{ Id: 100 }, { Id: 200 }, ] }],
    category, categoryGroup, found = false, i, j;

for (i = 0; i < categoryGroups.length ; i++) {
    categoryGroup = categoryGroups[i];
    for (j = 0; j < categoryGroup.Categories.length; j++) {
        category = categoryGroup.Categories[j];
        if (category.Id === categoryGroup.Id) {
            found = true;
            window.document.write("category " + category.Id); // swap
            break;                                            // lines
        }
    }
    if (found) break;
}

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

4 Comments

instead of Id:1.if have to print id:2.then what modification i have to do?
where do you get 2? is it just where id is the same, but the other Id in the array?
from the above code i m getting output "category 1".in same way suppose i have to print "category 2".the what i have to do?
i do not understand where do you get 2?
0

 var categoryGroups = [
        {
            Id: 1, Categories: 
             [
                { Id: 1 },
                { Id: 2 }, 
            ]

        },
        {
            Id: 2, Categories: [
                { Id: 100 },
                { Id: 200 },
            ]

        }
    ]
categoryGroups.forEach(function(categoryGroup){
  console.log(categoryGroup.Id);
  var category = categoryGroup.Categories.filter(function(category){
  return categoryGroup.Id == category.Id
  });
   category.length && console.log(category);
})

Comments

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.