0

I have an json array like this:

{"orders" : [ null, {
    "date" : "2012-03-19T07:22Z",
    "item" :
      {"name":"pizza", "quantity":"2", "size":"25"}, {"name":"suppe", "quantity":"2", "size":"1" }
  },{
    "date" : "2018-03-19T07:22Z",
    "item" : {"name":"brot", "quantity":"1", "size":"-"}, {"name":"hawaii", "quantity":"5", "size":"45" }
  } ]
}

And I would like render a nested list with vue but I don't understand why I can't. I already passed the data, first level data can render, but secont level can not.:

<ul v-for="order in orders">
       <li>{{order.date}}</li>
           <li>{{order.item}}</li>
              <ul v-for="i in orders.item">
                 <li>{{i.name}}</li>
               </ul>
           </li>
        </ul>
8
  • 1
    Your second version isn't an array. Do you mean, just remove the null entry? array = array.filter(x => x) Commented Jun 4, 2018 at 10:36
  • sorry, I fixed the question. Commented Jun 4, 2018 at 10:39
  • 1
    Now your second version is invalid JSON. Commented Jun 4, 2018 at 10:39
  • Sorry, I rather wrote the whole problem, because I am newbie Commented Jun 4, 2018 at 10:47
  • You have a typo i in orders.item should be i in order.item. Commented Jun 4, 2018 at 10:47

1 Answer 1

1

In your JSON item should be an array, not an object. Also, close your li tag:

new Vue({
  el: '#app',
  data: {
    orders: [
      null,
      {
        "date": "2012-03-19T07:22Z",
        "item": [{
            "name": "pizza",
            "quantity": "2",
            "size": "25"
          },
          {
            "name": "suppe",
            "quantity": "2",
            "size": "1"
          }
        ]
      }, {
        "date": "2018-03-19T07:22Z",
        "item": [{
            "name": "brot",
            "quantity": "1",
            "size": "-"
          },
          {
            "name": "hawaii",
            "quantity": "5",
            "size": "45"
          }
        ]
      }
    ]
  }
})
<script src="https://unpkg.com/vue"></script>

<div id="app">
  <ul v-for="order in orders" v-if="order != null">
    <li>{{order.date}}</li>
    <li>
      <ul v-for="i in order.item">
        <li>{{i.name}}</li>
      </ul>
    </li>
  </ul>
</div>

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

1 Comment

Thank you! I must be much carefullier with the brackets!

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.