Search code examples
arraysobjectvue.jscouchdbv-for

Vue.js v-for each object text with in an array or objects


I want to list out the text that is inside an array containing objects. I dont seem to be able to work out how to address this I can get close...

for example

<template>
  <div>
    <ul>
      <li v-for="(value, index) in otherclients" v-bind:key="index">
        DATA = {{ value.doc.notes }}
      </li>
    </ul>
  </div>
</template>

<script>
import { mapState } from 'vuex'

export default {
  name: 'Viewer',
  computed: mapState({
    otherclients: state => state.otherclients
  })
}
</script>

<style lang="css" scoped></style>

will output

  • DATA = [ { "id": "w5fpn80fnnf5nxdj9f1n1i", "text": "Welcome new device mydoc_android", "owner": "YOU", "deleted": false }, { "id": "w5fpn80fnnf5nxdj9f1wwwn1i", "text": "android 2", "owner": "YOU", "deleted": false } ]
  • DATA = [ { "id": "c1ds7zqd7tcgig0b1xs1q", "text": "Welcome new device mydoc_ios", "owner": "YOU", "deleted": false }, { "id": "nf5nxdj9f1dwwen1iw5fpn80fn", "text": "More Text", "owner": "YOU", "deleted": false } ]

but what I want is

  • Welcome new device mydoc_android
  • android 2
  • Welcome new device mydoc_ios
  • More Text

But I cant seem to work out the best way to access into this final layer. Any pointers appricaited. This is part of a bigger project so structure is set for state.otherclients and quite complex.


Solution

  • Use nested for loop with template tag

    <template>
      <div>
        <ul>
          <template v-for="(value, index) in otherclients">
             <li v-for="(note, note_index) in value.doc.notes">{{note.text}}</li>
          </template>
        </ul>
      </div>
    </template>
    

    var app = new Vue({
     el: '#app',
     data:{
      name:'niklesh',
      otherclients:[
        {doc:{notes:[ { "id": "w5fpn80fnnf5nxdj9f1n1i", "text": "Welcome new device mydoc_android", "owner": "YOU", "deleted": false }, { "id": "w5fpn80fnnf5nxdj9f1wwwn1i", "text": "android 2", "owner": "YOU", "deleted": false } ]}},
        {doc:{notes:[ { "id": "c1ds7zqd7tcgig0b1xs1q", "text": "Welcome new device mydoc_ios", "owner": "YOU", "deleted": false }, { "id": "nf5nxdj9f1dwwen1iw5fpn80fn", "text": "More Text", "owner": "YOU", "deleted": false } ]}},
      ]
     }
    });
    
    //[[{text:'test11'},{text:'test12'}]},[{text:'test21'},{text:'test22'}]}]
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.10/vue.js"></script>
    <div id="app">
          <ul>
          <template v-for="(value, index) in otherclients">
            <li v-for="(note, note_index) in value.doc.notes">{{note.text}}</li>
          </template>
          </ul>
     </div>