Thursday, 16 March 2017

How to Iterate through JSON to make ejs snippets with dynamic data

Alright,

I've found myself in over my head with this one (or it's late and I can't think, either / or).

I have a json response that looks like

-OBJ
  -Random Data
  -Results (array)
    -0
    -1 
    -2 
    -etc
      -data
         -product.title
         -product.whatever
           -value (array)
             -object 
               -stuff I'm trying to get

I need several of these fields, and I have these snippets

<div class="product clearfix">
  <div class="product-image">
    <div class="fslider" data-arrows="false">
      <div class="flexslider">
        <div class="slider-wrap">
          <div class="slide"><a href="#"><img src="<%- path to obj %>" alt="Dark Brown Boots"></a></div>
          <div class="slide"><a href="#"><img src=<%- path to obj %> alt="Dark Brown Boots"></a></div>
          <div class="slide"><a href="#"><img src="<%- path to obj %>" alt="Dark Brown Boots"></a></div>
        </div>
      </div>
    </div>
</div>
<div class="product-desc">
    <div class="product-title"><h3><a href="#"><%- path to title %></a></h3></div>


  </div>
</div>

How would I use something like this:

<% users.forEach(function(user){ %>
    <%- include('user/show', {user: user}); %>
  <% }); %>

to iterate through each array item in the JSON and create the product tiles with the unique data I need?

Thank you for your help!



via Quesofat

No comments:

Post a Comment