title: Nested foreach/forloops, looping through JSON to extract values
body: <p>I'm going to pre-face this with saying i'm not sure this is the best approach so other approaches are greatly appreciated </p>
  
  <p><em>End Goal: To store a list of products and the toppings purchased by calling the woocommerce API and using the response data</em></p>
  
  <p>I'm calling the woocommerce REST api that provides me a good chunk of JSON data back. In the JSON are line_items. These are the products purchased. Nested in line_items are meta_data, this is the toppings for example tomato or sauce.</p>
  
  <p>Attached an image of the JSON<a href="https://i.stack.imgur.com/hLwMm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hLwMm.png" alt="enter image description here"></a></p>
  
  <p>So what i'm trying to do is create something like this</p>
  
  <pre><code>var testOrderItems =
    [{
        title: "Fried Chicken Burger",
        meta: [
          "Lettuce",
          "cheese slice",
          "kethcup"
        ]
      },
      {
        title: "Beef Burger",
        meta: [
          "Lettuce",
          "cheese slice",
          "kethcup"
        ]
      }
    ]
  </code></pre>
  
  <p>which will follow my schema for oder items</p>
  
  <pre><code>var orderItems = new Schema({
      title: {type: String, required: true},
      meta: [{type: String}]
  
  });
  </code></pre>
  
  <p>So to do this, i figured I would just do a forloop or foreach through the JSON to get all the product names and their meta. Getting actual values is easy. The hard part is creating the array or JSON object that I can then store, i'm just not sure how to create it whilst in the loop. Below are a few things I tried</p>
  
  <pre><code>let fullData = JSON.parse(result)
  
  //parsed response from woocommerce API call
  
  fullData.line_items.forEach((product, index) =&gt; {
    //for each line item get me the product
    orderItems.push(product.name)
    //var namey = 
    //push the product name to the orderItemsArray
    product.meta_data.forEach(function(meta) {
      //checks for string as one of the plug-ins fills the meta with more nested information and we only want the top level string
      if (typeof meta.value === 'string' || meta.value instanceof String)
        // it's a string
        orderItems.push(meta.value)
      //Onbviously won't nest the meta with the product name just on new lines
    })
  });
  </code></pre>
  
  <p>The I thought I could do it in for loops by storing an ID ref as "i" and being able to re-reference this later in the nested loop to add the meta, i got a little lost with this</p>
  
  <pre><code>var length = fullData.line_items.length
  
  for (let i = 0; i &lt; length; i++) {
    // console.log(i);
    console.log(fullData.line_items[i].name)
    for (let j = 0; j &lt; fullData.line_items[i].meta_data.length; j++) {
      var metaValue = fullData.line_items[i].meta_data[j].value
      if (typeof metaValue === 'string' || metaValue instanceof String) {
       console.log(fullData.line_items[i].meta_data[j].value);
        stringMeta = fullData.line_items[i].meta_data[j].value
        //this works but has drawbacks
        //1 obviously just overwrites itself each time
        //2 will stop at the end of meta so won't add items without meta
        finalOrderItems = {
          id: i,
          name: fullData.line_items[i].name,
          meta: [stringMeta]
        }
      }
    }
  }
  </code></pre>
  
  <p>and thats where I am, feels like this should be incredibly easy but can't quite grasp it at the moment.</p>