[Vuejs]-Trying to display data but code is not working properly

0👍

Try this :

new Vue({
  el: '#app',
  data: {
    secondary: null
  },
  mounted() {
    this.invokeAPI();
  },
  methods: {
    invokeAPI() {
      // I am assigning the response directly but you can do it by making an API call
      const res = {"secondaryName":"","secondaryInstitution":"","secondaryNumber":"","secondaryNumber":""};

      const sItems = [];

      Object.entries(res).forEach(([key, val]) => {
        if (key.startsWith('s'))
          sItems.push({ label: key, value: val });
      });

      this.secondary = sItems;
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <div v-for="(item, index) in secondary" :key="index">
    <span class="label">
      {{ item.label}}
    </span>
    <span class="value">
      {{ item.value }}
    </span>
  </div>
</div>

Leave a comment