---
title: "javaScript nested for-in and for loops"  
description: "javaScript nested for-in and for loops"  
author: "abhi mohan"  
published: 2017-08-26  
updated: 2018-06-07  
canonical: https://www.mindstick.com/forum/34336/javascript-nested-for-in-and-for-loops  
category: "javascript"  
tags: ["javascript", "nested", "array", "loop", "object"]  
reading_time: 2 minutes  

---

# javaScript nested for-in and for loops

I am a [beginner](https://www.mindstick.com/forum/23159/need-beginner-project-ideas) in javaScript. Can someone please help me with the below [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) ?\
I want to write a [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) with for in and [for loop](https://www.mindstick.com/forum/12568/android-for-loop-not-working-in-main-thread) so that :\
It will loop through all [recipes](https://yourviews.mindstick.com/story/1619/best-bengali-recipes-easy-bengali-recipes) in the recipeBook, and [print](https://www.mindstick.com/blog/301752/types-of-3d-printing-technology) the recipe [name](https://yourviews.mindstick.com/view/87450/how-to-generate-a-brand-name-using-linkedin-comprehensive-guide) followed by its ingredients list. For example, the [output](https://www.mindstick.com/interview/34427/explain-the-output-in-angular) for the [chocolate cake](https://yourviews.mindstick.com/story/3730/easy-recipe-for-chocolate-cake) might look as follows:

```
Chocolate Cake:- 1 3/4 cup flour- 1/2 cup cocoa - 2 tsp baking powder - 1 cup Chelsea White Sugar - 125g butter - 2 Tbsp Chelsea Golden Syrup - 2 eggs - 1 1/2 cup milk - 2 tsp baking soda - 1 tsp vanilla essence
```

Code to add "chocolate cake" to the recipe book could look like this:

```
recipeBook["Chocolate Cake"] = [
  "1 3/4 cup flour",
  "1/2 cup cocoa",
  "2 tsp baking powder",
  "1 cup sugar",
  "125g butter",
  "2 Tbsp golden syrup",
  "2 eggs",
  "1 1/2 cup milk",
  "2 tsp baking soda",
  "1 tsp vanilla essence"
];
```

## Replies

### Reply by Prakash nidhi Verma

Hi folks, you should make a table like charts which is shown as you wants. than uses loops in/for or nested for type loops for recipeBook.

you can follow this code is shown below..

```
<html> <body>
<h1>Nested Loops</h1><p>The Nested for/in statement loop for chocolate cake recipeBook</p><p id="demo"></p><script>var y = [];y[0]= {quantity:1 3/4 fname:"cup", lname:"flour"}; //fname:first name, lname:last namey[1]= {quantity:1/2 fname:"cup", lname:"cocoa"}; y[2]= {quantity:2 fname:"tsp",mname:"backing",lname:"powder"}; y[3]= {quantity:1 fname:"cup",mname:"chelsea",lname:"white sugar"};y[4]= {quantity:125g fname:"butter"};.........
.........and so on as your choice....var x;for (x in chocolate) {    y += chocolate[x] + " ";}document.getElementById("Mindstick answer").innerHTML = y;</script></body></html>
```

define all variables correctly and it's another way to solve your problem. you should try this code for loop...

```
for (for (var i = 0; i < chocolate.length; i++) {     for (var j = 0; j < chocolate[i].length; j++) {        for (var k = 0; k < chocolate[i][j].length; k++) {            var.y(chocolate[i][j][k]);        }    }}
```

you can explore more in nested loop by this blog,which is given by our another mindstick member.

Link: https://www.mindstick.com/blog/390/using-javascript-nested-loops

glad to help, i hope it will helpful for you.

Happy Coding :)


---

Original Source: https://www.mindstick.com/forum/34336/javascript-nested-for-in-and-for-loops

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
